|
|
||
Patrick Keegan's BlogBinding JComboBox and Getting a Reasonable Display ValuePosted 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
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.
Probably not what you want! To fix this, you could override the 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.
When you run the application again, you should then get something that looks much more reasonable.
Bookmark blog post: CommentsComments are listed in date ascending order (oldest first) | Post Comment
| ||
|
|