Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in JComboBox

 
Anagha Medicharla
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic