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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • 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
Hi.
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 :
[/code]
{ to be displayed for user...
Hashtable cuisineTable = new Hashtable();
int jj = 0;
while (//condition is true)
{ cuisineTable.put(new Integer(jj), cuisineCountry);
jj++;
//test and set condition
}
int var_nn = jj;
[/code]
TIA :-)
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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_xRGB", "INT_ARGB", "INT_ARGB_PRE", "INT_BGR",
"3BYTE_BGR", "4BYTE_ABGR", "4BYTE_ABGR_PRE", "USHORT_565_RGB",
"USHORT_x555_RGB", "BYTE_GRAY", "USHORT_GRAY",
"BYTE_BINARY", "BYTE_INDEXED", "BYTE_BINARY 2 bit", "BYTE_BINARY 4 bit",
"INT_RGBx", "USHORT_555x_RGB"};
static JComboBox screenCombo;
static JComboBox screenComboHash;
Hashtable hashScreenValue;

public JComboEx() {

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

for (int i = 0; i < screenNames.length; i++) {
screenCombo.addItem(screenNames[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));
screenComboHash.setLightWeightPopupEnabled(true);

Enumeration enum = hashScreenValue.elements();
while(enum.hasMoreElements()){
String value = (String)enum.nextElement();
screenComboHash.addItem(value);
}

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();
cex.setSize(400,400);
cex.setVisible(true);
}
}
Bye,
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!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic