0

如何使用 JGoodies 将保存字符串项的 JComboBox 绑定到 bean 中的整数值
我想在 JComboBox 名称中显示并将其绑定到该名称的某个 ID 值。

4

1 回答 1

0

使用 MVP 架构,您将拥有一个 Presentation SelectInList ,它保留您要列出的对象列表:例如:

class MyObject {
    private Integer id;
    private String name;
    ...
    //getters and setters
}

class MyView {
    private MyPresentationModel;
    private JComboBox myComboBox;
    ...
    private void buildComponents {
        myComboBox = BasicComponentFactory.createComboBox(getPresentationModel().getMyObjectsSelectionInList(), new ListCellRenderer() {

            protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

            @Override
            public Component getListCellRendererComponent(JList list, Object value,
                    int index, boolean isSelected, boolean cellHasFocus) {
                JLabel renderer = (JLabel) defaultRenderer
                        .getListCellRendererComponent(list, value, index, isSelected,
                            cellHasFocus);

                renderer.setText(((MyObject) value).getName()); //this is point
                return renderer;
            }
        });
    }
}

class MyPresentationModel extends com.jgoodies.binding.PresentationModel {
    private SelectionInList myObjetcsSelectionInList;
    private List<MyObject> list;
    private MyModel myModel;

    public MyPresentationModel(MyModel myModel) {
        this.myModel = myModel;
        list = //LOAD LIST
    }

    public SelectionInList getPeriodTypeSelectionInList() {
        if (myObjetcsSelectionInList == null) {
            myObjetcsSelectionInList = new SelectionInList(list.toArray(new MyObejct[list.size()]), getModel(MyModel.PROPERTY_MY_OBJECT));
            myObjetcsSelectionInList.setSelectionIndex(0);
        }
        return myObjetcsSelectionInList;
    }
    ...
}

class MyModel {
    static public String PROPERTY_MY_OBJECT = "myObject";
    private MyObject myObject;

    // getters and setters
}
于 2014-06-06T13:25:18.030 回答