Sergiu Toporjinschi
Sergiu Toporjinschi

Reputation: 1

How to bind a property of a composing object

I'm new in SwingBindings.

I have a list of beans named Project each Project has an reference to another bean named Config. Each Config has 2 attributes, how can I bind with swingbindings those two attributes of the bean Config to components form a jframe?

the list of projects is bound to a combobox. When I change the selected item from combobox(of project) I want to refresh the values from the object config associated to the current selected project in the form.

Upvotes: 0

Views: 156

Answers (1)

Harry Joy
Harry Joy

Reputation: 59650

Try this:

  1. Create your own cell renderer that implements ListCellRenderer.
  2. Assign that renderer to comboBox containing Projects. (comboBox.setRenderer(...))
  3. Now you can directly add your Project instance into comboBox and also get that back.
  4. In ListCellRenderer in getListCellRendererComponent(.....) mehod cast value (Second argument) to Project and return appropriate value that you want to display in comboBox. (Might be project's name/title)
  5. Add listener to comboBox. (May be ItemListener) and in its action method get selected object of comboBox, which will be your Project object.
  6. From this project object you can easily get Config (as you said you have reference to Config object in Project object) object and set data in frame.

Upvotes: 2

Related Questions