Search |
||
Binding JComboBox's Elements and Selected ItemPosted by pkeegan on May 22, 2008 at 7:08 PM PDT
This is part 3 in a series of posts that I'm doing to show how to use beans binding and JPA to create a Java desktop database application. In this installment, I concentrate on combo boxes, namely how to populate combo boxes from a table and then how to bind the user selection to a record. If you want to code along with me, be sure to read my previous two posts. When we created the skeleton for this application, we (or, rather, the New Java Desktop Application wizard) did not take into account the foreign key from the Clients table to the Countries table. The only relationship between tables that was acknowledged was the one pertinent to the master/detail relationship between the Clients and the Orders tables. So now we will need to take some extra steps to establish the Clients/Countries relationship in the entity classes:
We also need to update the column binding for the country field so that it refers to the
Now it's time to do the binding for the Country combo box in the dialog.
The combo box is almost ready to work properly in the dialog. It is set up to draw its values from the Countries db table, and the item that the user selects is then applied to the country field in the current record. However, we still need to customize the rendering of the combo box, since the values bound to the combo box are Countries objects, not simple names. We will do that by specifying a custom cell renderer. (For JTables and JLists, the beans binding library enables you to specify display expressions, thus avoiding the need to create a custom renderer, but that feature does not exist yet for combo boxes.) To get the combo boxes to render country names, do the following:
The combo box should be ready to go - except for one thing. It doesn't have any values to display yet. You can go ahead and populate the table with a few SQL commands and then run the project. Or you can indulge me in this digression that demonstrates how you can quickly do this with a few hacks within the IDE (and shows you some handy features along the way). First, create a separate form for adding countries to the db by doing the following:
We have just essentially created another application with its own main class. In order to properly run this class, we need to temporarily make it the main class of the project. (Simply using the Run File command won't work since this command doesn't pick up classpath dependencies.) We can do so by creating a new project configuration.
You can now start editing the list of countries.
Once you have some countries in the the Countries table, you can run the main application and see the combo box in action:
The application works, but it's still very rough around the edges. Here is some quick tidying up we can do now:
We still have some work to do, such as:
I'll cover those topics and others in ensuing posts. Where time and personal knowledge allows, I'll try to field requests as well. »
Comments
Comments are listed in date ascending order (oldest first)
Submitted by benhur99ph on Sun, 2008-06-15 19:23.
What about if I want to use a List box instead of the dropdown combo box? I tried making the application from the start again and I modified the SQL statement so that the "countries" in the "Client" table is set to string. When I get to making the Jdialog (EditClient), if I use a textbox for the countries (like the others) it works fine. I tried using a List box and I have successfully binded the elements property and when I run the application, it shows the list of countries from the countries table. But when I bind the selectedElement property, when I run it, I get the ClassCastException. How can I make it work? Is it a mistake that I modified the SQL statement? Thanks!
Submitted by dags on Fri, 2008-05-23 10:38.
There is any advice against doing a customized toString() method in Countries instead of using CountryListCellRenderer ?. I know that CountryListCellRenderer is better if you want to display icons and text but a customized toString() seems better for simple cases.
Submitted by pkeegan on Fri, 2008-05-23 12:14.
Yes, you can use toString() for this case. I don't do it because toString() can be used for any number of things, so I wanted to keep the separation. But that might be worth calling out in the final version of the tutorial.
Submitted by bestage on Sat, 2008-09-13 13:21.
Hello Patrick,
first of all thanks for the nice tutorial.
I did learn a lot how to handle things. As a novice in Java I do have to learn a lot after .NET and Visual Studio. But I like it and yourt tutorial showed that it is not such a long way to get where I want to get.
I have one question. After playing with your tutorial I went further and added a combobox to the JTable (Custom editor).
For example, I have a table called Customers and it has a join column AddressID connected with the entity Address. Now, the combox is populated with Address-Objects correctly. But when I select on Address I get the ClassCastException.
OK the solution may be to override the getCellEditorValue() method of my custom editor.
But from here I have no Idea what I should do in the overridden method.
Can you give me a hint?
Thanks
Submitted by pkeegan on Mon, 2008-09-15 03:02.
Hmm, I guess I had would have to see your code to know for sure what is going wrong. Have you created both a custom editor and custom renderer for the combo box in the table column (and then specified the editor and renderer in the Table Contents/Columns dialog)? You might be merely missing the custom renderer.
Submitted by yoguess on Tue, 2008-09-16 00:19.
hi patrick,
i`m new to Gui using netBeans. i have build the whole project as suggested...
now i have done some changes. instead of selecting "database Application", i have selected "basic Application" then made master/detail sample form.
i have then kept some text fields and comboboxes. binded it as u have suggested. everything is working fine except when i select any item from combobox, the item is not getting saved in database....
please suggest me....
thanks in adv
Submitted by pkeegan on Thu, 2008-09-18 04:07.
Hi yoguess,
In this blog entry, the Save functionality isn't yet set up. You will have follow the next entry to see that work. Also note that the event model is different. The Swing Application Framework uses enhanced action support, whereas the master/detail form uses straight event handlers, so there is some variation there.
Submitted by asubhan on Sun, 2009-04-05 21:25.
Hi Patrick,
I am using netbeans 6.5 and create a simple application containing Customers table and Coutries table.
I generated the application using master/detail skeleton and in detail option page I select textfields rather than table option to utilize the editing component. I replace CountryId textfield component with JcomboBox. I don't have dififculty to showing proper country list on the JCombobox once the application being run but everytime I selec an item on the JCombobox the changing was not reflected on the Jtable.
Please advise me what was wrong?
since I don't have similar problem when the editing component were place on the separate form (JDialog Form)
|
||
|
|