将对象列表插入组合框

时间:2013-12-13 21:05:45

标签: java javafx javafx-2 javafx-8

我有这个组合框,它将对象列表插入到组合框中:

private List<ListGroupsObj> lisGroups;

    public static class ListGroupsObj
    {

        private int groupId;
        private String groupName;

        public static ListGroupsObj newInstance()
        {
            return new ListGroupsObj();
        }

        public ListGroupsObj()
        {
        }

        public ListGroupsObj groupId(int groupId)
        {
            this.groupId = groupId;
            return this;
        }

        public ListGroupsObj groupName(String groupName)
        {
            this.groupName = groupName;
            return this;
        }

        public int getGroupId()
        {
            return groupId;
        }

        public String getGroupName()
        {
            return groupName;
        }

        //        @Override
//        public String toString()
//        {
//            return serverName;
//        }
    }


ListGroupsObj ob = ListGroupsObj.newInstance().groupId(12).groupName("Group12");
        ListGroupsObj osb = ListGroupsObj.newInstance().groupId(13).groupName("Group13");
        ListGroupsObj oa = ListGroupsObj.newInstance().groupId(14).groupName("Group14");
        ListGroupsObj oz = ListGroupsObj.newInstance().groupId(15).groupName("Group15");

final ComboBox<ListGroupsObj> listGroups = new ComboBox();
listGroups.getItems().addAll(ob, osb, oa, oz);

我想做这样的事情:

listGroups.getItems().addAll(listGroups);

如何将对象列表插入组合框?

1 个答案:

答案 0 :(得分:4)

<强>更新

我的错,错过了javaFX标签并得出结论......

你应该做的事情就像......

final ComboBox<ListGroupsObj> listGroups = new ComboBox();
listGroups.getItems().addAll(ob, osb, oa, oz);

final ComboBox<ListGroupsObj> otherGroups = new ComboBox(listGroups.getItems());

这会将一个组合框的项目传递给其他组合。

但你也应该能够做点像......

List<ListGroupsObj> listOfObjects = new ArrayList<>(5);
listOfObjects.add(ListGroupsObj.newInstance().groupId(12).groupName("Group12"));
listOfObjects.add(ListGroupsObj.newInstance().groupId(12).groupName("Group13"));
listOfObjects.add(ListGroupsObj.newInstance().groupId(12).groupName("Group14"));
listOfObjects.add(ListGroupsObj.newInstance().groupId(12).groupName("Group15"));

ObservableList<ListGroupsObj> observableListOfObjects = FXCollections.observableList(listOfObjects);

final ComboBox<ListGroupsObj> listGroups = new ComboBox(observableListOfObjects);
final ComboBox<ListGroupsObj> otherGroups = new ComboBox(observableListOfObjects);

或者

final ComboBox<ListGroupsObj> listGroups = new ComboBox();
listGroups.setItems(observableListOfObjects);

或者

final ComboBox<ListGroupsObj> listGroups = new ComboBox();
listGroups.getItems().addAll(observableListOfObjects);

这当然需要进行测试;)

请查看ComboBox JavaDocs了解详情

已更新为可运行示例

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TestComboBox extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        ListGroupsObj ob = ListGroupsObj.newInstance().groupId(12).groupName("Group12");
        ListGroupsObj osb = ListGroupsObj.newInstance().groupId(13).groupName("Group13");
        ListGroupsObj oa = ListGroupsObj.newInstance().groupId(14).groupName("Group14");
        ListGroupsObj oz = ListGroupsObj.newInstance().groupId(15).groupName("Group15");

        final ComboBox<ListGroupsObj> listGroups = new ComboBox();
        listGroups.getItems().addAll(ob, osb, oa, oz);

        final ComboBox<ListGroupsObj> otherGroups = new ComboBox(listGroups.getItems());

        List<ListGroupsObj> listOfObjects = new ArrayList<>(5);
        listOfObjects.add(ListGroupsObj.newInstance().groupId(12).groupName("Group12"));
        listOfObjects.add(ListGroupsObj.newInstance().groupId(12).groupName("Group13"));
        listOfObjects.add(ListGroupsObj.newInstance().groupId(12).groupName("Group14"));
        listOfObjects.add(ListGroupsObj.newInstance().groupId(12).groupName("Group15"));
        ObservableList<ListGroupsObj> observableListOfObjects = FXCollections.observableList(listOfObjects);
        listGroups.setItems(observableListOfObjects);

        otherGroups.getItems().addAll(observableListOfObjects);

        VBox root = new VBox();
        root.getChildren().add(btn);
        root.getChildren().add(listGroups);
        root.getChildren().add(otherGroups);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be launched
     * through deployment artifacts, e.g., in IDEs with limited FX support.
     * NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public static class ListGroupsObj {

        private int groupId;
        private String groupName;

        public static ListGroupsObj newInstance() {
            return new ListGroupsObj();
        }

        public ListGroupsObj() {
        }

        public ListGroupsObj groupId(int groupId) {
            this.groupId = groupId;
            return this;
        }

        public ListGroupsObj groupName(String groupName) {
            this.groupName = groupName;
            return this;
        }

        public int getGroupId() {
            return groupId;
        }

        public String getGroupName() {
            return groupName;
        }

        //        @Override
//        public String toString()
//        {
//            return serverName;
//        }
    }

}