[英]JavaFX - How to set value to a textfield inside a tab?
I am trying to set value to a text field which is inside a Tab. 我试图将值设置为选项卡内的文本字段。 I am having multiple tabs and I want to set value to text field inside each tab. 我有多个标签,我想为每个标签内的文本字段设置值。 Any idea as to how to set the text for textfield inside the tab? 关于如何在标签内设置文本字段的文本有任何想法吗? I am using the below code to update the value for the textfield, but nothing's happening while trying to do the same. 我正在使用下面的代码来更新文本字段的值,但是在尝试执行相同操作时却什么也没有发生。
Code: 码:
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;
}
Updated Code including the Pojo class for FXML. 更新了代码,包括用于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/Property Class 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);
}
}
I am still not able to set the value for text field vendorHeader1. 我仍然无法设置文本字段vendorHeader1的值。 Can someone point out what's going wrong? 有人可以指出出什么问题了吗?
You have to apply a Binding
between the text property of the TextField
and the SimpleStringProperty
that is used for the value. 您必须在TextField
的text属性和用于该值的SimpleStringProperty
之间应用Binding
。 You will have to make the vendor header property of your FXMLProps
public in a way that enables Binding
options in other classes: 您必须以一种启用其他类中的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;
}
}
Then somewhere in your application (maybe in start()
) you need to create the Binding
like 然后在某处你的应用程序(也许在start()
你需要创建Binding
像
// bind those two properties (TextField, SimpleStringProperty)
Bindings.bindBidirectional(vendorHeader1.textProperty(), fxmlProps.vendorHeaderProperty());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.