X

Geertjan's Blog

  • February 12, 2008

Beans Binding Via The Road Less Travelled By (Part 2)

Geertjan Wielenga
Product Manager
A continuation from yesterday—this time, instead of a JTable and a JTextField, we'll look at a JList and a JTextField. I realized yesterday, after writing that blog entry, that it was pretty dumb to set the JTextFields as not editable while the update strategy was READ_WRITE. Had I made the JTextFields editable, I would have been able to update the currently selected row in the table, from the related JTextFields. So this time I won't make that mistake. As yesterday, we will not use any tool at all. Just a plain editor.

When we finish this blog entry, we will have a small JFrame that looks like this:

As you can see above, when you select something in the JList, the current value will appear in the JTextField. In addition, when you change something in the JTextField, the JList will be updated, as below where I changed "Oak" to "Pine":

Firstly, set everything up as yesterday, with the database and so on. Then, similar but different to yesterday, we bind the result from the query to a JList. However, instead of ColumnBindings (which apply to JTables), we use a DetailBinding to get the data from the database via the entity class:

//Constructor:
public DemoJList() {
setSize(new Dimension(400, 400));
setTitle("Demo Frame");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
samplePUEntityManager = javax.persistence.Persistence.createEntityManagerFactory("samplePU").createEntityManager();
customerQuery = samplePUEntityManager.createQuery("SELECT c FROM Customer c");
customerList = customerQuery.getResultList();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
JListBinding jListBinding = SwingBindings.createJListBinding(
AutoBinding.UpdateStrategy.READ_WRITE, customerList, jList1);
jListBinding.setDetailBinding(ELProperty.create("${name}"));
bindingGroup.addBinding(jListBinding);
jScrollPane1.setViewportView(jList1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
bindingGroup.bind();
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DemoJList().setVisible(true);
}
});
}

Next, add the JTextField, declare it and instantiate it, then bind it to the JList and add it to the ContentPane like this, above the line where the BindingGroup is bound (i.e., right above the 2nd to last line in the DemoJList constructor above):

Binding binding = Bindings.createAutoBinding(
AutoBinding.UpdateStrategy.READ_WRITE,
jList1,
ELProperty.create("${selectedElement.name}"),
jTextField1,
BeanProperty.create("text"));
bindingGroup.addBinding(binding);
getContentPane().add(jTextField1, java.awt.BorderLayout.SOUTH);

Run the class and it'll look and work as shown at the start of this blog entry.

Now you can experiment a bit. Change the binding that you see above to the following. Here I've highlighted the changes I made:

Binding binding = Bindings.createAutoBinding(
AutoBinding.UpdateStrategy.READ,
jList1,
ELProperty.create("${selectedElement.email}"), this,
BeanProperty.create("title"));
bindingGroup.addBinding(binding);

Now, whenever you select a new value in the JList, the titlebar of the JFrame will show the related e-mail address. Try a few similar experiments to get a feel for how the binding with the JList works.

Join the discussion

Comments ( 7 )
  • Mark Ashworth Wednesday, February 13, 2008

    Good Day Geertjan,

    Thank you for the tutorial. I was wondering how to handle when an object is added to the java.util.List that the JList is bound to.

    Are we suppose to:-

    1. Unbind and rebind

    2. Raise a property change for the java.util.List

    3. Must we find a way with indexed properties

    Regards,

    Mark P Ashworth


  • Geertjan Wednesday, February 13, 2008

    Mark, I'm sorry but I really don't know the answer to this question. I would recommend writing to a general Java discussion forum with this.


  • Patrick Keegan Friday, February 15, 2008

    Regarding Mark's question, nothing special needs to be done when an object is added to the JList. This is part of the point of beans binding. The bound properties stay in synch so you don't have to do anything.

    Also note that the binding is to the JList object itself, not the set of objects contained by the JList.


  • Ante Sabo Wednesday, May 7, 2008

    Hi,

    I am using beans binding for our GUI project and here is one thing that could help us a lot when we want to change background color of components which are not valid. Now we name each binding and this way we know to which GUI element we have connection, but it would help us a lot if we would not be naming our bindings and have

    ELProperty.getExpression()

    Is this possible to have in future versions ? I am affraid to make myself this method into class, and also do not want base software on ELProperty.toString() method


  • Fazileh Saturday, December 6, 2008

    Hi,

    I'm writing an application that bind a database table with a JList. But I want to use where clause. how do I have to do it?

    Thank you.

    Fazileh


  • Ed Thursday, February 12, 2009

    High, Fazileh and Geert,

    I have the same problem. I'm quit able to connect a database to a JList component. This creates 3 objects.

    - An Entity Manager

    - An Query

    - A List to represent the result of the Query to the JList

    The Query in Qeury - Properties page looks like this:

    SELECT s FROM Carrental s

    Which promptly return a list of Cartypes (I have a demo database installed to do some testing in order to get this done) and I have used cartype as the database column to display in the list

    However when I edit the code into:

    SELECT DISTINCT s FROM Carrental s everything works fine and the complete list is displayed which can be expected since DISTINCT is now looking for distinct records not distinct car types.

    SELECT DISTINCT s.cartype FROM Carrental s should do the trick but nog the JList remains empty?

    I'm quit new to NetBeans and I'm using version 6.5. I have done some analysing and the query is stored under the resources tab of the Source Packages and in a file called ....PK

    I've spend the bigger part of a day in getting this (seemingly trivial) task to work. Anyone?

    Greetings,

    Ed


  • Elias Gomes Thursday, October 20, 2011

    Muito bom seu tutorial.

    Procurei muito sobre o assunto na web. Finalmente tirei minhas dúvidas.

    Estava tentando usar jgoodies binding mas, ele é muito mais complicado.

    Parabéns.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.