aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Populate JComboBox from a database via JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Populate JComboBox from a database via JDBC" Watch "Populate JComboBox from a database via JDBC" New topic

Populate JComboBox from a database via JDBC

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As the title says, I am trying to populate a JComboBox from a database table. I've read suggestions that I should implement my own ComboBoxModel. So far I have successfully done this. However, once the JComboBox is created the contents are static.

Now I want to modify my ComboBoxModel to respond dynamically to changes in the underlying table. What is a good way to go about this? Are there any design patterns that might apply (besides the obvious MVC that I'm already using).

Ultimately, the JComboBox will appear in a window that appears only when the user clicks on a menu item. The window includes other controls to enter data that will ultimately end up in a database table. My idea is to use JComboBox for any foreign keys in the table. This way the user sees a textual representation of the data rather than just the ID number that is really being used in the underlying table.

At the moment, I am using JOptionPane to display the window (using a JPanel as the "message"). One idea I have is to query the database each time the window opens. I see two problems here:

1) There is a performance hit since the user will most likely open the same window multiple times without changing the table that the foreign key refers to.

2) I'm not sure how to detect when JOptionPane displays the window since I don't think it's possible to register a WindowListener with it.

Does anyone have any suggestions what I can do here? Please let me know if I need to post any code or additional information about the design of my project.



Java API Documentation
The Java Tutorial
Amit Siinngghh

Joined: Jun 19, 2004
Posts: 12
Try the Observer pattern

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Amit Ssiinngghh:
Try the Observer pattern


Thanks for the suggestion. I am familiar with the Observer pattern. It is very similar (if not identical) to the Listeners that AWT uses for event handling. I think I can see how to implement the Observer interface in my DatabaseComboBoxModel. However, I'm still not sure how to implement the Observerable interface. How will the class that implements Observable be able to tell if the underlying table has changed? I have some budding ideas here, but I'm uncertain if I want to go in that direction since it seems like it will become overly complicated rather quickly.

I think for now, I'm going to have the ComboBox update its content every time it is displayed. I know this isn't the most efficient way to go, so if I have time later I'll look at the Observer pattern in more detail.

I agree. Here's the link:
subject: Populate JComboBox from a database via JDBC
Similar Threads
Passed SCJD Bodgitt and Scraper: 353
Beginner Problem: son & parent
Building a GUI with dynamically changeable "inner view" of different components.
cannot find symbol
Change & sort the contents of JComboBox in Swing