• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in JComboBox

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic