Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Howto: load a JComboBox list from a Hashtable?  RSS feed

 
achana chan
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 :-)
 
Purvi Pandya
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...
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing forum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!