This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes ComboBox Item Display Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ComboBox Item Display" Watch "ComboBox Item Display" New topic

ComboBox Item Display

Danie Van Eeden

Joined: Sep 03, 2003
Posts: 13
Ive got a JComboBox component, placed on a panel with gridbaglayout which in turn is placed on a JTabbedPane.
I populate the combobox through coding from a SQL server database.
My problem is that none of the records that i add to the combobox are being displayed. When i check the recordcount, it is equal to the number of records added. I am also able to use the info in the combo box via referencing it. eg. Class.setTitle(Combobox.getItemAt(0).toString());
the Title is thus set to the appropriate record, but still I cant see these records in the combo box itself.
another thing: I sequencially add my containers. First I add the Combobox to a panel with gridbaglayout. then I add the TabbedPane with this panel. then i add the TabbedPane to the JFrame. If i populate the Combobox before adding it to the form, everything is fine, otherwise not.

quite odd to me. any help would be appreciated

Regards<br />-dve83-
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
A code sample from your application would be useful here, but let me suggest one potential problem [that I've run into before]. Make sure that when you populate your "Combobox" with the live data, that you are updating the existing instance and not creating a new instance.
In the following code there is a problem:

The first combobox is created and added to the frame, and then another combo box is created with the real data. What you would want to do is either create the combobox with the real data, or do something like this:

But this is only a guess w/out seeing your code.
Danie Van Eeden

Joined: Sep 03, 2003
Posts: 13
hi and thanks
my code is as follows:
got a function in different class (say as follows):
public JComboBox getdata (Recordset localrs)
JComboBox localCombo = new JComboBox();
while (!localrs.getEOF())
now in main class i have something like this:
inside actionEvent methods:
JComboBox mainCombo = new JComboBox();
Recordset mainrs = new Recordset();
mainCombo = getdata(mainrs);
the data in mainrs is retrieved elsewhere, but it does definitely contain data. im using visual J++ and when i check data values in runtime through debug-type window thingy, i can see hat the data has been added but itjust wont display.
something just started to mess with my head. could it be that JCombobox and Recordset (without J) are not compatible. I think im going to check this out anyway
all help appreciated
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
I think what you are seeing is an example of what I talked about in my first reply. If you look at the code in the "actionEvent" method:

The first line, "JComboBox mainCombo = new JComboBox()" creates an instance of a JComboBox and assigns it to "mainCombo". Two lines later, "mainCombo = getdata(mainrs)" will create a second instance of a JComboBox and assign that to "mainCombo".
The second instance (created by the call to "getdata(...)" is the instance that contains the data. The first instance, no longer explicitly referenced in the code, may still be around (if there is a reference to it), but it contains no data.
So the question becomes, which instance of "JComboBox" that you created is in the UI? In fact, if the three lines of code [cited above] are in a method, then the "mainCombo" instance variable will be local only to that method and whatever appears in the UI will be another--a third--instance of "JComboBox".
If it's not too much trouble, provide the full code [at least in terms of the instance variables, UI building, local methods] and it should be easy to spot the errant code.
Danie Van Eeden

Joined: Sep 03, 2003
Posts: 13
thanks, couldn't replay in the last few weeks. but I appreciate the help. thats exactly it. another instance of Combobox was made and the original one just never got the data. I finally figured i could pass the Combobox to populate as a parameter to a publiv void and it changes accordingly. thanks
jQuery in Action, 2nd edition
subject: ComboBox Item Display
Similar Threads
getting jcombobox mouse over value
On button click populate combobox
JComboBox doubt
Problem with GridBagLayout Manager while adding any component
Comment on GUI..