JavaFX应用程序中的ComboBox不显示数据 [英] ComboBox in JavaFX application doesn't show the data
问题描述
我上课了:
public class Element {
private final IntegerProperty id;
private final StringProperty name;
...constructors...
public Integer getId() {
return id.get();
}
public void setId(Integer id) {
this.id.set(id);
}
public IntegerProperty idProperty() {
return id;
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
public StringProperty nameProperty() {
return name;
}
@Override
public String toString() {
return name.get();
}
我尝试在ComboBox中显示这些元素的列表。在我的控制器中,我有:
And I try to display List of those elements in ComboBox. In my controller I have:
@FXML
private ComboBox<Element> combo;
然后我在函数中有以下代码填充其他GUI元素:
And then I have following code in function that fill other GUI elements:
ObservableList<Element> elements = FXCollections.observableArrayList(ElRep.getElements());
combo = new ComboBox<Element>(elements);
combo.getSelectionModel().selectFirst();
我也尝试过:
combo.setItems(elements);
似乎没有任何效果。我得到空的ComboBox。
and nothing seems to work. I get empty ComboBox.
推荐答案
你应该从不初始化带注释的字段 @ FXML
:该注释的关键是该对象是作为加载FXML文件的一部分创建的,并被注入控制器。如果您创建一个新对象,您将不再引用FXML加载器创建的对象(并显示在UI中):因此您对该对象属性所做的任何更改都不会显示在UI中。
You should never initialize fields annotated @FXML
: the point of that annotation is that the object is created as part of loading the FXML file and is injected into the controller. If you create a new object, you will no longer be referencing the object created by the FXML loader (and displayed in the UI): so any changes you make to that object's properties will not appear in the UI.
因此,省略对新ComboBox<>(...)
的调用,并使用 combo.setItems(。 ..)
(或 combo.getItems()。setAll(...)
)初始化现有的组合框:
So omit the call to new ComboBox<>(...)
and use combo.setItems(...)
(or combo.getItems().setAll(...)
) to initialize the existing combo box:
示例控制器:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
public class Controller {
@FXML
private ComboBox<Element> combo ;
public void initialize() {
ObservableList<Element> elements = FXCollections.observableArrayList(
new Element(1, "Element 1"),
new Element(2, "Element 2")
);
combo.setItems(elements);
combo.getSelectionModel().selectFirst();
}
}
FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.ComboBox?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
<top>
<ComboBox fx:id="combo" />
</top>
</BorderPane>
测试:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Test extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("sample.fxml")), 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这篇关于JavaFX应用程序中的ComboBox不显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!