The Source for Java Technology Collaboration
User: Password:



Patrick Keegan

Patrick Keegan's Blog

Binding JComboBox and Getting a Reasonable Display Value

Posted by pkeegan on October 26, 2007 at 02:58 AM | Comments (11)

Just a few days after I published my first major foray into explaining Beans Binding in NetBeans, I received some feedback asking how to populate a JComboBox with reasonable display values from a data source. I had been wondering the same thing. With the help of Honza Stola (explanation plus code snippet) and the trusty Beans Binding Javadoc (available from NetBeans by choosing Help > Javadoc References > Beans Binding), here's my stab at it.

First some background. The Beans Binding library supplies special classes for binding JTables, JLists, and JComboBox components with data from another source (such as a database).

For JTable objects, you use the JTableBinding class to bind the table to a List object. The objects contained in the List object correspond with rows in the JTable. You can then use JTableBinding.ColumnBinding to map specific columns from the data source to the JTable columns. (The IDE helps you generate all of this code, whether you are using the Bind dialog box or you are using the New Java Desktop Application template in the New Project Wizard.)

For JList objects, you use the JListBinding class to bind a List of object to the JList component. Since each item in the JList corresponds with an object rather than a simple display value, you might need to use JListBinding.DetailBinding to map a property from the objects in the List to the display value of the JList. In the IDE, you have JListBinding.DetailBinding code generated by opening the Bind dialog box and filling in the Display Expression field with an EL expression to refer to a property of the bound List object. In the image below, the Expression Source is a List object that contains objects that represent all of the rows in the Customer database table. The Display Expression field is filled with an EL expression that causes the displayed values to be derived from the name property of the Customer entity class (which represents the NAME column in the CUSTOMER database table).

jlist-bind-dialog.png

There is also a JComboBoxBinding class in the Beans Binding library, which enables you to bind a List object to a combo box. Unfortunately, as of Beans Binding 1.1.1, there is not yet a DetailBinding class that enables you to specify how to derive the values that are displayed in the JComboBox. If you were to bind a combo box to a database table and then run the application, you would get a combo box that looks something like the following image.

bind-without-setting-detail.png

Probably not what you want! To fix this, you could override the toString() method of the bean you are binding. The disadvantage there is that the toString() method might need to be used by other things than just your JComboBox. A better way to handle this is to customize how the cell is rendered. Here's some boilerplate code for that approach:

jComboBox1.setRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (value instanceof MyEntityClass) {
                   MyEntityClass mec = (MyEntityClass)value;
                   setText(mec.getPropertyFromMyEntityClass());
               }
               return this;
           }
       })

You can add that code in the IDE's GUI Builder by right-clicking the combo box, choosing Customize Code, and typing the code just below where the combo box is instantiated. The image below shows the Customize Code dialog with the code I have added and customized to work my Customer entity class.

customcode.png

When you run the application again, you should then get something that looks much more reasonable.

bind-with-detail.png


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Now, how can I bind Jcombo with a detail table?

    Posted by: dpaglia on November 23, 2007 at 01:54 AM

  • I put a jTable in the frame. Then in the jTable1.bind elements dialog I try with:
    binding source: jComboBox1
    binding Expression: ${selectedItem.myentityCollection}
    combobox works fine, but nothing appers in the table

    Posted by: dpaglia on November 23, 2007 at 02:44 AM

  • Hello Patrick


    Thanks so much - how to bind a Combo Box to a Database Field....

    I honestly despaired of ever finding a way to do this in NetBeans - and it's a fundamental requirement for me
    It seems so hard to do some basic things in Swing - I could do this stuff in Delphi in 1999....

    Additional gripe...

    Must say I'm getting a wee bit tired of repeated NetBeans 'Master/Detail' tutorials using a single table - I can't imagine what use that could ever be in the real world..

    However - NetBeans is still superb....

    Good Job...

    Thanks


    Jim Fergusson
    Yorkshire England

    Posted by: waverley on December 12, 2007 at 09:30 AM

  • Thanks for the feedback, Jim. Indeed, I plan to start writing a more complex example soon (after the holidays). Hopefully, it will be ready some time in January.

    Posted by: pkeegan on December 12, 2007 at 09:37 AM

  • Hello Patrick

    Apologies for the terrible formatting....

    Regards
    Jim Fergusson

    Posted by: waverley on December 12, 2007 at 09:45 AM

  • Hello Patrick

    Look forward to a more complex example

    I'm so pleased with the Combo Box !
    You have made my day - actually you have made my week!
    Regards


    Jim

    Posted by: waverley on December 12, 2007 at 09:48 AM

  • Hi Patrick,
    You mention "For JTable objects, you use the JTableBinding class to bind the table to a List object. "

    Can you give a little example/tutorial how to do this? I'd like to bind a jTable to a collection of objects, but all the netbeans examples assume you are using the persistence API which I am not - just a List of ordinary objects in my app.

    Thanks!
    Colin.

    Posted by: colinbankier on December 13, 2007 at 08:37 PM

  • Actually - I've worked out how to get the UI to do it all for me...then I can just see what it did. Thanks anyway.

    Posted by: colinbankier on December 14, 2007 at 12:17 AM

  • What if I want to bind the Combo Box to a database as well? For example: Using the Cars example, I want to standardize the Body Style from a seperate table.

    Posted by: marfia on January 30, 2008 at 06:54 AM

  • Hi! Patrick, do you have complex examples binding a JTable to Swing controls (JText, JComboBox)? , Thanks in advanced!

    Posted by: chanozamora on March 29, 2008 at 07:39 AM

  • Patrick,

    I hope you can help. I am following your tutorial to create a JCombo box using netbeans 6.1 that will populate the selection from a database table of a specific column. I have 2 questions.

    1) When I keyed in the code you provided into the custom code of JCombo I get this error.
    symbol : class DefaultListCellRenderer
    location: class sampleapp.SampleView
    jComboBox1.setRenderer(new DefaultListCellRenderer () {
    1 error

    2) How do I chose specific column to populate the JCombo box. Currently I am getting the first column ?

    Thank you in advance for your reply.

    Posted by: new_beans on June 23, 2008 at 10:53 PM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds