2015-11-29 56 views
0

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

我有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标记,它是要进行编辑。元素映射包含它假定从中获取文本的按钮ID和TextView ID。有点像“将特定按钮绑定到特定文本字段”。

private void changeText(ActionEvent event) throws IOException { 
Object source = event.getSource(); 
       Button clickedButton = (Button) source; 
       System.out.println(HTMLtags.get(clickedButton.getId())); 
       System.out.println(elementsMap.get(clickedButton.getId())); 
       writeToHTML(HTMLtags.get(clickedButton.getId()), elementsMap.get(clickedButton.getId())); 
} 

上面的方法可以正确地检索需要编辑的按钮和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 textFieldName = new TextField(); 
       textFieldName.setId(textField); 
       head.text(textFieldName.getText()); 

我这部分检索文本框的文本希望动态地执行此操作,而不通过 @FXML私有TextField“fx:id的名称”声明每个FXML元素并通过循环匹配每个元素。

我想做类似于通过ActionEvent获取按钮的ID /值的方式。

编辑: 这里是全FXML BTW

<?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> 

回答

0

您可以执行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(); 
+0

通过场景搜索时,Id查找返回null。即使我在FXML文件中设置了id =“newsTitle”。另外,当试图通过控制器访问场景时,我得到了nullpointerexception。 –

+0

我修复了空指针异常,但查找函数仍然返回空id。 –

+0

TextField未添加到场景中,或者查找无法找到该ID。当你进行查找时,ID前缀是否为#?这是必需的。 – Prometheus