簡體   English   中英

JGoodies綁定:使用JCombobox和默認值綁定對象

[英]JGoodies Binding: binding an object with JCombobox and default values

我有以下問題:

我想將View中的JCombobox與模型“ DomainModel”綁定,以便以后可以通過“ AnotherModel”獲得它。getModel();

我編寫了自己的CellRenderer,使其看起來像“ ID-名稱”。 但是,當我選擇組合框的值並調用“ AnotherModel” .getModel時,該值未保存在其中。

用JGoodies綁定綁定復雜的數據類型是不可能的嗎? 使用String可以正常工作,但我想綁定“ DomainModel”對象

這是簡化的代碼:

風景:

public class View extends JPanel {

    private JComboBox<DomainModel> cmbValueModel;

    public View(AntotherModel antotherModel,  List<DomainModel> manyDomainModels) {

    PresentationModel<DomainModel> domainModel = new PresentationModel<DomainModel>();
    domainModel.setBean(antotherModel.getModel());

    cmbValueModel = BasicComponentFactory.createComboBox(new SelectionInList<DomainModel>(manyDomainModels, domainModel.getModel(DomainModel.PROPERTYNAME_NAME)));

    Bindings.bind(cmbValueModel, new SelectionInList<>(), "");
    cmbValueModel.setRenderer(new DefaultListCellRenderer(){

        @Override
        public Component getListCellRendererComponent(JList<?> list,
                Object value, int index, boolean isSelected,
                boolean cellHasFocus) {

            return super.getListCellRendererComponent(list, value == null ? null : ((DomainModel)value).getId() + " - " + ((DomainModel)value).getName() , index, isSelected,
                    cellHasFocus);
        }

    });

    }

}

域:

public class DomainModel extends Model{

public static final String PROPERTYNAME_NAME = "name";

@GeneratedValue
private int id;
private String name;

public void setName(String name) {
    String oldVal = this.name;
    this.name = name;
    changeSupport.firePropertyChange(PROPERTYNAME_NAME, oldVal, name);
}

public String getName(){
    return name;
}

public int getId(){
    return id;
}

}

另一種型號:

    public class AntotherModel extends Model{

        public static final String PROPERTYNAME_MODEL = "model";

        private int id;
        private DomainModel model;


        public int getId() {
            return id;
        }
        public DomainModel getModel() {
            return model;
        }
        public void setId(int id) {
            this.id = id;
        }
        public void setModel(DomainModel model) {
            DomainModel oldVal = this.model;
            this.model = model;
            changeSupport.firePropertyChange(PROPERTYNAME_MODEL, oldVal, model);
        }



    }

您的代碼無法編譯或運行,因此很難弄清您要執行的操作。 如果您要這樣做,下面的示例將顯示一個組合框綁定到DomainModels列表的方法; 您可以將valueChangeListener添加到selectionInList中,以在用戶更改選擇內容時執行某些操作。 高溫超導

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.list.SelectionInList;

public class View extends JPanel {

    private JComboBox comboBox;

    public View(AnotherModel anotherModel, List<DomainModel> manyDomainModels) {
        final SelectionInList<DomainModel> selectionInList = new SelectionInList<DomainModel>(manyDomainModels);
        comboBox = BasicComponentFactory.createComboBox(selectionInList);

        comboBox.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                return super.getListCellRendererComponent(list, value == null ? null : ((DomainModel) value).getId() + " - "
                    + ((DomainModel) value).getName(), index, isSelected, cellHasFocus);
            }
        });

        add(comboBox);
    }

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        final AnotherModel anotherModel = new AnotherModel();
        final List<DomainModel> manyDomainModels = new ArrayList<DomainModel>();
        final DomainModel domainModel1 = new DomainModel();
        domainModel1.setName("foo");
        final DomainModel domainModel2 = new DomainModel();
        domainModel2.setName("bar");
        final DomainModel domainModel3 = new DomainModel();
        domainModel3.setName("baz");
        manyDomainModels.add(domainModel1);
        manyDomainModels.add(domainModel2);
        manyDomainModels.add(domainModel3);
        frame.getContentPane().add(new View(anotherModel, manyDomainModels));
        frame.pack();
        frame.setVisible(true);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM