• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ComboBox Item Display

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
localrs.movefirst();
while (!localrs.getEOF())
{
localCombo.addItem(localrs.getFields("Name").getString());
localrs.movenext();
}
}
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
danie
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Beauty is in the eye of the tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic