Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javafx中通过id动态查找FXML变量?_Java_Html_Javafx_Fxml - Fatal编程技术网

如何在javafx中通过id动态查找FXML变量?

如何在javafx中通过id动态查找FXML变量?,java,html,javafx,fxml,Java,Html,Javafx,Fxml,我正在尝试创建一个HTML编辑器(学习目的),并试图使程序尽可能动态。我想通过它的id查找FXML变量,比如TextField,并在按下按钮时检索TextField中的文本 我设置了2个映射变量 public FXMLDocumentController() { this.HTMLtags = new HashMap<>(); HTMLtags.put("pressMeButton", "h2.fx-h2"); HTMLtags.pu

我正在尝试创建一个HTML编辑器(学习目的),并试图使程序尽可能动态。我想通过它的id查找FXML变量,比如TextField,并在按下按钮时检索TextField中的文本

我设置了2个映射变量

 public FXMLDocumentController() {
        this.HTMLtags = new HashMap<>();
        HTMLtags.put("pressMeButton", "h2.fx-h2");
        HTMLtags.put("setNewsMessageButton", "p.fx-message");

        this.elementsMap = new HashMap<>();
        elementsMap.put("pressMeButton", "newsTitle");
        elementsMap.put("setNewsMessageButton", "newsMessage");
    }
上面的方法正确检索需要编辑的按钮和HTML标记的ID

下面是在HTML中设置文本的代码

private void writeToHTML(String HTMLTag, String textField) {
        File input = new File(filePath);
        Document doc;
        try {
            doc = Jsoup.parse(input, "UTF-8");

            Element head = doc.select(HTMLTag).first();
            originalText = head.toString();

            TextField textFieldName = new TextField();
            textFieldName.setId(textField);
            head.text(textFieldName.getText());


            System.out.println("Original Text is: " + originalText);
            System.out.println("Modified Text is: " + head);

            Path path = Paths.get(filePath);
            Charset charset = StandardCharsets.UTF_8;

            String content = new String(Files.readAllBytes(path), charset);
            content = content.replaceAll(originalText, head.toString());
            Files.write(path, content.getBytes(charset));

        } catch (IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
问题是,我不知道如何在这部分代码中从textfield中查找和检索文本

TextField textFieldName = new TextField();
                textFieldName.setId(textField);
                head.text(textFieldName.getText());
我希望在不通过声明每个FXML元素的情况下动态地执行此操作 @FXML私有文本字段“fx:id的名称”,并通过循环匹配每个元素

我希望这样做与通过ActionEvent获取按钮ID/值的方式类似

编辑: 这里是完整的FXML顺便说一句

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.66" xmlns:fx="http://javafx.com/fxml/1" fx:controller="newsletter.editor.FXMLDocumentController">
   <children>
      <ScrollPane fitToWidth="true" prefHeight="800.0" prefWidth="1280.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <content>
            <VBox alignment="CENTER" prefHeight="450.0">
               <children>
                  <HBox alignment="CENTER">
                     <children>
                        <Label alignment="CENTER" minHeight="25.0" minWidth="192.0" prefHeight="50.0" prefWidth="192.0" text="Newsletter Title" wrapText="true">
                           <font>
                              <Font size="18.0" />
                           </font>
                        </Label>
                        <TextField id="newsTitle" fx:id="newsTitle" prefHeight="28.0" prefWidth="180.0" promptText="Write here" HBox.hgrow="ALWAYS" />
                     </children>
                  </HBox>
                  <HBox prefHeight="100.0" prefWidth="200.0">
                     <children>
                        <TextArea fx:id="newsMessage" prefHeight="100.0" prefWidth="766.0" />
                     </children>
                  </HBox>
                  <HBox prefHeight="100.0" prefWidth="200.0" />
                  <HBox prefHeight="100.0" prefWidth="200.0" />
                  <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
                     <children>
                        <Button fx:id="setNewsMessageButton" mnemonicParsing="false" onAction="#changeText" text="MAGIC" />
                        <Button fx:id="pressMeButton" mnemonicParsing="false" onAction="#changeText" text="Press Me for Magic">
                           <HBox.margin>
                              <Insets right="10.0" />
                           </HBox.margin>
                        </Button>
                        <Button fx:id="filePathButton" mnemonicParsing="false" onAction="#filePathSave" text="Select Path">
                           <HBox.margin>
                              <Insets right="10.0" />
                           </HBox.margin>
                        </Button>
                        <Button fx:id="popoverButton" mnemonicParsing="false" onAction="#popOverTrigger" text="PopOver Test">
                           <HBox.margin>
                              <Insets right="10.0" />
                           </HBox.margin>
                        </Button>
                     </children>
                  </HBox>
               </children>
               <padding>
                  <Insets left="20.0" right="20.0" />
               </padding>
            </VBox>
         </content>
      </ScrollPane>
   </children>
</AnchorPane>

您可以执行id查找以查找
文本字段

Parent parent = getTextFieldParent(); // the Parent (or Scene) that contains the TextFields
TextField textField = (TextField) parent.lookup("#myTextField");
if (textField != null)
    String text = textField.getText();

您可以执行id查找以查找
TextField

Parent parent = getTextFieldParent(); // the Parent (or Scene) that contains the TextFields
TextField textField = (TextField) parent.lookup("#myTextField");
if (textField != null)
    String text = textField.getText();

是的,您需要方法
lookup()
。不仅父类有这个方法,它也有at面板类,如FlowPane、AnchorPane和许多其他类。在场景类中也可以使用此方法

但是!此方法仅在场景显示后进行查看。我现在不知道,为什么会这样,但在场景
show()
之前,
lookup()
只找到顶级面板。在
show()
之后,它按id查找元素,如
scene.lookup(“#myId”)
需要符号
#

另外,
lookup()
方法可以按类型查找没有id的元素


示例:
scene.lookup(“流程窗格”)(不带#)将在.fxml文件中找到第一个流窗格。和
scene.lookupAll(“ImageView”)
将返回场景中包含所有ImageView元素的海量图像。

是的,您需要的方法是
lookup()
。不仅父类有这个方法,它也有at面板类,如FlowPane、AnchorPane和许多其他类。在场景类中也可以使用此方法

但是!此方法仅在场景显示后进行查看。我现在不知道,为什么会这样,但在场景
show()
之前,
lookup()
只找到顶级面板。在
show()
之后,它按id查找元素,如
scene.lookup(“#myId”)
需要符号
#

另外,
lookup()
方法可以按类型查找没有id的元素


示例:
scene.lookup(“流程窗格”)(不带#)将在.fxml文件中找到第一个流窗格。和
scene.lookupAll(“ImageView”)
将返回场景中包含所有ImageView元素的海量数据。

Id lookup在搜索场景时返回null。即使我在FXML文件中设置了id=“newsttitle”。另外,当试图通过控制器访问场景时,我得到了nullpointerexception。我修复了nullpointer异常,但查找函数仍然返回null id。可能是TextField未添加到场景中,或者查找无法找到id。在查找时,id是否以#为前缀?这是必须的,是的。我像这样做TextField TextField=(TextField)parent.lookup(“#”+myTextField”);其中myTextField是传递给方法的变量。执行该行时,我得到ClassCastException:Collections&EmptySet不能强制转换为TextFieldId lookup在搜索场景时返回null。即使我设置了id=“newsTitle“在FXML文件中。另外,当试图通过控制器访问场景时,我得到了nullpointerexception。我修复了nullpointer异常,但查找函数仍然返回null id。可能是TextField未添加到场景中,或者查找无法找到id。在查找时,id是否以#为前缀?这是必须的,是的。我是这样做的:TextField=(TextField)parent.lookup(“#”+myTextField”);其中myTextField是传递给方法的变量。当执行该行时,我得到ClassCastException:Collections&EmptySet不能转换为TextFieldRoman,what
show()
正在做的是应用css并运行布局过程以完全初始化场景图形。在节点连接到场景(不需要为此显示或显示场景)后,通过在节点上调用
applyCss()
layout()
,可以在不显示的情况下获得相同的效果。之后,
lookup()
by CSS选择器将按预期工作(请参阅info.jewelsea,感谢您的评论,现在我明白了。Roman,
show()
所做的是应用CSS并运行布局过程来完全初始化场景图。通过调用
applyCss()
layout(),您可以在不显示的情况下获得相同的效果
在节点连接到场景后(不需要为此显示或显示场景)。之后,CSS选择器的
lookup()
将按预期工作(请参阅info.jewelsea,感谢您的帮助)