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>
通過場景搜索時,Id查找返回null。即使我在FXML文件中設置了id =「newsTitle」。另外,當試圖通過控制器訪問場景時,我得到了nullpointerexception。 –
我修復了空指針異常,但查找函數仍然返回空id。 –
TextField未添加到場景中,或者查找無法找到該ID。當你進行查找時,ID前綴是否爲#?這是必需的。 – Prometheus