0

I have the following problem:

I want to bind a JCombobox in my View with my model 'DomainModel', so that I can get later get it with 'AnotherModel'.getModel();

I wrote an own CellRenderer to make it look like 'ID - Name'. But when I select the value of the Combobox and I call 'AnotherModel'.getModel the value isn't saved in it.

Isn't it possible to bind complex datatypes with JGoodies Bindings? With String it works fine but I want to bind an object of 'DomainModel'

Here is the simplified code:

The View:

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

    });

    }

}

The domain:

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

}

Another model:

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



    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

Your code doesn't compile or run so it's rather hard to figure out what you are trying to do. The example below displays a combobox binding to a list of DomainModels if this is what you are after; you can add a valueChangeListener to the selectionInList to do something when the user changes the selection. HTH

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);
    }
}
Matthew Wise
  • 2,639
  • 26
  • 23