gpt4 book ai didi

JavaFX - 如何为选项卡内的文本字段设置值?

转载 作者:行者123 更新时间:2023-12-02 11:06:27 26 4
gpt4 key购买 nike

我正在尝试将值设置为选项卡内的文本字段。我有多个选项卡,我想为每个选项卡内的文本字段设置值。知道如何设置选项卡内文本字段的文本吗?我正在使用下面的代码来更新文本字段的值,但尝试执行相同操作时没有任何反应。

代码:

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 的文本属性和用于该值的 SimpleStringProperty 之间应用 Binding。您必须以在其他类中启用 Binding 选项的方式将 FXMLProps 的供应商 header 属性公开:

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());

关于JavaFX - 如何为选项卡内的文本字段设置值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50922942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com