GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes problem in JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in JComboBox" Watch "problem in JComboBox" New topic
Author

problem in JComboBox

Anagha Medicharla
Greenhorn

Joined: Feb 06, 2001
Posts: 11
Hi everybody,
I am using JComboBox for user to select state in US.what i want to do is if user selects 'ALABAMA' then text in combobox should set to 'AL'.Does anybody know how to do tis?

Thanx in advance.
selvas kumars
Ranch Hand

Joined: Jan 06, 2001
Posts: 115
hi,
what you can do is get the item selected by
using getSelectedItem() of JComboBox and and call toString() method on it and call the substring(0,2) on it...
Now you will get only first 2 characters..
combo.setSelectedItem((.combo.getSelectedItem().toString()).substring(0,2));
try out this ........
silva
Anagha Medicharla
Greenhorn

Joined: Feb 06, 2001
Posts: 11
Hi Selvas,
Thanks for your reply. I tried your code.
But it's throwing java.lang.StackOverflowError.
I have written the code as folllows.
jcomboState.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(ItemEvent e) {
jcomboState_itemStateChanged(e);
}
});
void jcomboState_itemStateChanged(ItemEvent e) {
jcomboState.setSelectedItem((jcomboState.getSelectedItem().toString()).substring(0,2));
}

Can you help me out further.Why is it throwing that exception?
Anagha

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Hi,
I feel that u get the erreor due to the following reason:
Since u r chaing the selected item at
void jcomboState_itemStateChanged(ItemEvent e)
{
jcomboState.setSelectedItem((jcomboState.getSelectedItem().toString()).substring(0,2));
}
This code will again trigger the jcomboState_itemStateChanged method and this happens infinitely
Hope u got it
 
GeeCON Prague 2014
 
subject: problem in JComboBox