Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

Howto: load a JComboBox list from a Hashtable?

Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does one load a JComboBox list from entries in a Hashtable?
For example, I can create and load a list in the following JComboBox:

and I have this Hashtable with a list of all cuisine which I intend to load into cuisineString :
{ to be displayed for user...
Hashtable cuisineTable = new Hashtable();
int jj = 0;
while (//condition is true)
{ cuisineTable.put(new Integer(jj), cuisineCountry);
//test and set condition
int var_nn = jj;
TIA :-)
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
below code is to load JComboBox from String[] and Hashtable both
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class JComboEx extends JFrame {
static String[] screenNames = {
"Auto Screen", "On Screen", "Off Screen",
"INT_RGBx", "USHORT_555x_RGB"};
static JComboBox screenCombo;
static JComboBox screenComboHash;
Hashtable hashScreenValue;

public JComboEx() {

screenCombo = new JComboBox();
screenCombo.setPreferredSize(new Dimension(120, 18));

for (int i = 0; i < screenNames.length; i++) {
// first i have to add item to the hash table
// bcoz at present i can't show with database

hashScreenValue = new Hashtable();
for (int i = 0; i < screenNames.length; i++) {
hashScreenValue.put(new Integer(i), screenNames[i]);

screenComboHash = new JComboBox();
screenComboHash.setPreferredSize(new Dimension(120, 18));

Enumeration enum = hashScreenValue.elements();
String value = (String)enum.nextElement();

JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );

topPanel.add( screenCombo, BorderLayout.NORTH );
topPanel.add( new JLabel("Hello !!!"), BorderLayout.CENTER );
topPanel.add( screenComboHash, BorderLayout.SOUTH );
public static void main(String args[]){

JComboEx cex = new JComboEx();
Purvi Pandya
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Purvi, for your time and input.
It is always appreciated.
After some late evening work, I came up with this little piece of code which works :

The puzzling thing is the vrey first entry (index = 0) in the JCombobox is a null string.
Its not a show-stopper, since I've set the setSelectedIndex(1) to 1.
But it would be nice to know...
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing forum.
Anderson gave himself the promotion. So I gave myself this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic