Javafx:使用FXML的可重用集合
我想将一个集合绑定到FXML中的多个ChoiceBox。 但是,我知道的唯一方法是使用:Javafx:使用FXML的可重用集合,java,javafx,collections,fxml,Java,Javafx,Collections,Fxml,我想将一个集合绑定到FXML中的多个ChoiceBox。 但是,我知道的唯一方法是使用: 是否可以在控制器中声明集合并在FXML中引用它,而不是为每个ChoiceBox复制集合?您可以在控制器中定义项目: public class Controller { private ListProperty<String> choiceBoxItems = new SimpleListProperty(FXCollections.observableArrayList());
是否可以在控制器中声明集合并在FXML中引用它,而不是为每个ChoiceBox复制集合?您可以在控制器中定义项目:
public class Controller {
private ListProperty<String> choiceBoxItems = new SimpleListProperty(FXCollections.observableArrayList());
public Controller() {
IntStream.range(1,10).mapToObj(i -> Integer.toString(i))
.forEach(choiceBoxItems::add);
}
public ListProperty<String> choiceBoxItemsProperty() {
return choiceBoxItems ;
}
public ObservableList<String> getChoiceBoxItems() {
return choiceBoxItemsProperty().get() ;
}
public void setComboBoxItems(ObservableList<String> choiceBoxItems) {
choiceBoxItemsProperty().set(choiceBoxItems) ;
}
// ...
}
然后在每个选项框中引用:
它可以在控制器或任何其他类中,如(示例为组合框。同样可以应用于choicebox): combo.fxml
<?import javafx.scene.control.ComboBox?>
<ComboBox fx:id="combo1" items="${itemLoader.items}" prefWidth="150.0"
xmlns:fx="http://javafx.com/fxml/1" >
</ComboBox>
装载机类别
public class ComboLoader {
private ObservableList<String> obsStrings;
public ComboLoader() {
obsStrings = FXCollections.observableArrayList(createStrings());
}
private List<String> createStrings() {
return IntStream.rangeClosed(0, 5)
.mapToObj(i -> "String "+i)
.map(String::new)
.collect(Collectors.toList());
}
//name of this method corresponds to itemLoader.items in xml.
//if xml name was itemLoader.a this method should have been
//getA(). A bit odd
public ObservableList<String> getItems(){
return obsStrings;
}
}
公共类组合加载器{
私有可观察列表字符串;
公共组合加载器(){
obstrings=FXCollections.observearraylist(createStrings());
}
私有列表createStrings(){
返回IntStream.rangeClosed(0,5)
.mapToObj(i->“字符串”+i)
.map(字符串::新建)
.collect(Collectors.toList());
}
//此方法的名称对应于xml中的itemLoader.items。
//如果xml名称为itemLoader.a,则此方法应为
//getA()。有点奇怪
公共可观察列表getItems(){
返回字符串;
}
}
用以下方法进行测试:
public class ComboTest extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
primaryStage.setTitle("Populate combo from custom builder");
Group group = new Group();
GridPane grid = new GridPane();
grid.setPadding(new Insets(25, 25, 25, 25));
group.getChildren().add(grid);
FXMLLoader loader = new FXMLLoader(getClass().getResource("combo.fxml"));
loader.getNamespace().put("itemLoader", new ComboLoader());
ComboBox<String>combo = loader.load();
grid.add(combo, 0, 0);
Scene scene = new Scene(group, 450, 175);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
公共类ComboTest扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage)引发IOException{
setTitle(“从自定义生成器填充组合”);
组=新组();
GridPane grid=新建GridPane();
网格设置填充(新插图(25,25,25,25));
group.getChildren().add(网格);
FXMLLoader=newFXMLLoader(getClass().getResource(“combo.fxml”);
loader.getNamespace().put(“itemLoader”,newcomboloader());
ComboBoxcombo=loader.load();
添加(组合,0,0);
场景=新场景(组,450,175);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}
您可以使用参照现有对象的fx:id
。使用此标记,您可以重用可观察列表
: