• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JComboBox drop-down event

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working a project which uses a lot of JComboBox.The selection items in JComboBox are from database. I need to keep the selection list always consistent with the relevant information in the database.That's to say, whenever the users click on JComboBox, it should trigger an event to retrieve latest information from the database. I tried FocusEvent, but it doesn't work. Java tutorial suggested not to use lower-level events(like Key or mouse) for ComboBox. Can anyone tell me which event and event listener should I use? I found an article which said I should use ListSelectionListener, but there is no sample code. I tried it and it doesn't work. Please help!!!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FocusEvent should work. Can you show us the code that didn't work? Of course the data would update every time it got focus, even if the user was just tabbing past it.
ListSelectionEvent would by weird because the user would have to click on an option in the list and THEN the list would update - kinda strange.
 
Angie Jin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply. My code is like this:
.....
private void updateRoleCombo()
{
String[] avrole = new String[]{""};
try {
avrole = (_parent._security).getRoleList(_parent._token); // retrieve the new list from the database
} catch (RemoteException ex2) {
JOptionPane.showMessageDialog(_parent, ex2, "Error",
JOptionPane.ERROR_MESSAGE);
}
_role.removeAllItems();
for (int i = 0; i < avrole.length; i++)
{
_role.addItem((String)(avrole[i]));
}
_role.setSelectedIndex(0);
}
......
_role = new JComboBox(rolelist);
add(_role);
_role.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
updateRoleCombo();
}
public void focusLost(FocusEvent e) {
}
});
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic