[英]JavaFX - How to set value to a textfield inside a tab?
我試圖將值設置為選項卡內的文本字段。 我有多個標簽,我想為每個標簽內的文本字段設置值。 關於如何在標簽內設置文本字段的文本有任何想法嗎? 我正在使用下面的代碼來更新文本字段的值,但是在嘗試執行相同操作時卻什么也沒有發生。
碼:
public class FXMLController {
@FXML
private Button inputXmlFileBtn;
@FXML
private TextField inputXmlName;
@FXML
private TabPane xmlData;
@FXML
private Tab vendorHeaderFb;
@FXML
private TextField vendorHeader1;
Label label;
public String inputXmlFileChooser() throws ParserConfigurationException,
SAXException, IOException, JAXBException {
FileChooser fileChooser = new FileChooser();
// Set extension filter
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("XML files (*.xml)", "*.xml"));
// Open Dialog
File file = fileChooser.showOpenDialog(null);
String xmlPath = "";
xmlPath = file.getPath();
// Set the path for inputXmlName text field
if (file != null) {
inputXmlName.setText(xmlPath);
}
//Unmarshall
label = this.unmarshallXml();
System.out.println(label.getVendorHeader1());
vendorHeaderFb = new Tab();
vendorHeader1 = new TextField();
vendorHeader1.setText(label.getVendorHeader1());
vendorHeaderFb.setContent(vendorHeader1);
return xmlPath;
}
更新了代碼,包括用於FXML的Pojo類。
public class FXMLController {
@FXML
private Button inputXmlFileBtn;
@FXML
private TextField inputXmlName;
@FXML
private TabPane xmlData;
@FXML
private Tab vendorHeaderFb;
@FXML
private TextField VendorHeader1;
Label label;
public String inputXmlFileChooser() throws ParserConfigurationException,
SAXException, IOException, JAXBException {
FileChooser fileChooser = new FileChooser();
// Set extension filter
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("XML files (*.xml)", "*.xml"));
// Open Dialog
File file = fileChooser.showOpenDialog(null);
String xmlPath = "";
xmlPath = file.getPath();
// Set the path for inputXmlName text field
if (file != null) {
inputXmlName.setText(xmlPath);
}
//Unmarshall
label = this.unmarshallXml();
System.out.println(label.getVendorHeader1());
FXMLProps fxmlProps = new FXMLProps();
fxmlProps.setVendorHeader1(label.getVendorHeader1());
System.out.println(fxmlProps.getVendorHeader1());
VendorHeader1 = new TextField();
VendorHeader1.setText(fxmlProps.getVendorHeader1());
//vendorHeaderFb.setContent(vendorHeader1);
//vendorHeader1.setText(label.getVendorHeader1());
//vendorHeaderFb.setContent(vendorHeader1);
return xmlPath;
}
POJO /屬性類
public class FXMLProps {
private final SimpleStringProperty VendorHeader1 = new SimpleStringProperty(
"");
public FXMLProps() {
}
public FXMLProps(String VendorHeader1) {
setVendorHeader1(VendorHeader1);
}
public String getVendorHeader1() {
return VendorHeader1.get();
}
public void setVendorHeader1(String vH1) {
VendorHeader1.set(vH1);
}
}
我仍然無法設置文本字段vendorHeader1的值。 有人可以指出出什么問題了嗎?
您必須在TextField
的text屬性和用於該值的SimpleStringProperty
之間應用Binding
。 您必須以一種啟用其他類中的Binding
選項的方式公開FXMLProps
的vendor標頭屬性:
public class FXMLProps {
private final SimpleStringProperty vendorHeader = new SimpleStringProperty("");
public FXMLProps() {}
public FXMLProps(String vendorHeader) {
setVendorHeader(vendorHeader);
}
public String getVendorHeader() {
return VendorHeader1.get();
}
public void setVendorHeader(String vendorHeaderText) {
vendorHeader.set(vendorHeaderText);
}
// this is needed for the Binding
public final SimpleStringProperty vendorHeaderProperty() {
return vendorHeader;
}
}
然后在某處你的應用程序(也許在start()
你需要創建Binding
像
// bind those two properties (TextField, SimpleStringProperty)
Bindings.bindBidirectional(vendorHeader1.textProperty(), fxmlProps.vendorHeaderProperty());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.