Binding JComboBox's Elements and Selected Item

by Patrick Keegan

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:

  1. Create an entity class for the Countries table by right-clicking the package that contains your classes and choosing New | Entity Classes from Database.
  2. In the generated Countries class add the following line below the @ID annotation, just as we did for the Clients and Orders classes.
        @GeneratedValue(strategy=GenerationType.IDENTITY)
    
  3. Press Ctrl-Shift-I to add the necessary import statements.
  4. Modify the Clients entity class so that countryId property is of type Countries instead of Integer and that it is joined with the Countries db table. The following changes are necessary:
    • Replace this field declaration and annotations
          @Column(name = "COUNTRY_ID")
          private Integer countryId;
      

      with this code:

          @JoinColumn(name = "COUNTRY_ID", referencedColumnName = "COUNTRY_ID")
          @ManyToOne
          private Countries countryId;
      
    • Change the type of the getCountryId() method from Integer to Countries.
    • In the getCountryId() method, change the types of countryId and countryId from Integer to Countries.
    • Press Ctrl-Shift-I to add the imports for the pasted code.

We also need to update the column binding for the country field so that it refers to the country property of the Countries object instead of an Integer. (Code to use a country ID Integer was generated by the project since the skeleton was generated without having an entity class for the COUNTRIES table. If we don't make a change here, a ClassCastException will be thrown when you run the application.) Here are the steps:

  1. In the main form, right-click the top table and choose Table of Contents and then click the Columns tab.
  2. In the customizer, select the Country Id row.
  3. Change the Expression to ${countryId.country}. After you do so, the type should also change to String. masterdetail3-changeTableContents.png
  4. Change the Title from Country Id to Country (this affects the column heading in the running application).

Now it's time to do the binding for the Country combo box in the dialog.

Read the rest of this tutorial.

Comments:

coding to transfer data to the sql database from a selected item in swing combo box without using EJB.

Posted by nagashree on March 11, 2009 at 07:09 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

Java Technology Fundamentals are now covered as part of the Core Java Technology Tech Tips. Please go there for more tech tips.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today