Javafx Dynamically load ComboBox

强颜欢笑 提交于 2019-12-13 07:38:38

问题


I am trying to make a custom builder proposed in Dan Nicks's comment to this question.
The idea is to set combo's data before constructing it.
The fxml file:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ComboBox?>

<ComboBox  fx:id="combo1" items="${itemLoader.items}"  prefWidth="150.0" 
   xmlns:fx="http://javafx.com/fxml/1">
</ComboBox>

The class that provides the data:

public class ComboLoader {

    public ObservableList<Item> items;

    public ComboLoader() {

        items = FXCollections.observableArrayList(createItems());
    }

    private List<Item> createItems() {
            return IntStream.rangeClosed(0, 5)
                    .mapToObj(i -> "Item "+i)
                    .map(Item::new)
                    .collect(Collectors.toList());
        }

    public ObservableList<Item> getItems(){

        return items;
    }

    public static class Item {

        private final StringProperty name = new SimpleStringProperty();

        public Item(String name) {
            this.name.set(name);
        }

        public final StringProperty nameProperty() {
            return name;
        }

    }
}

And the test:

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();
        ComboBox combo = loader.load(getClass().getResource("combo.fxml"));
        loader.getNamespace().put("itemLoader", new ComboLoader());
        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);
    }
}

No errors produced, but combo is not populated.
What is missing ?


BTW: a similar solution for works fine:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.cell.PropertyValueFactory?>

 <TableView items="${itemLoader.items}"   xmlns:fx="http://javafx.com/fxml/1">
     <columns>
         <TableColumn text="Item">
             <cellValueFactory><PropertyValueFactory property="name" /></cellValueFactory>
         </TableColumn>
     </columns>
 </TableView>

回答1:


After fixing wrong loading and adding a toString method it works fine :

public class ComboLoader {

    private ObservableList<Item> obsItems;

    public ComboLoader() {

        obsItems = FXCollections.observableArrayList(createItems());
    }

    private List<Item> createItems() {
            return IntStream.rangeClosed(0, 5)
                    .mapToObj(i -> "Item "+i)
                    .map(Item::new)
                    .collect(Collectors.toList());
    }
    //name of this methods corresponds to itemLoader.items in fxml.
    //if xml name was itemLoader.a this method should have been
    //getA(). A bit odd 
    public ObservableList<Item> getItems(){

        return obsItems;
    }

    public static class Item {

        private final StringProperty name = new SimpleStringProperty();

        public Item(String name) {
            this.name.set(name);
        }

        public final StringProperty nameProperty() {
            return name;
        }

        @Override
        public String toString() {
            return name.getValue();
        }
    }
}


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


来源:https://stackoverflow.com/questions/44321738/javafx-dynamically-load-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!