• 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Veto Change in JComboBox

 
Ranch Hand
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know if there is a way to keep the selected index of a JComboBox from changing if certain criteria are not met when the selection changes?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you may have to supply a bit more info - I'm reading this as how to stop the selection change after the selection is changed,
which makes no sense.

some possibilities
1) you can disable the comboBox until conditions are right
2) you can reset the selected index back, after first removing the listener,
so it won't fire again, then ading it back after the reset.
 
Sheriff
Posts: 22510
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that, if you really need to ignore a selection made by the user, you can do so by using your own ComboBoxModel. It's the setSelectedItem method that is called when a user selects a value, so your implementation can intercept that call and do nothing if needed.
 
Charles Burton
Ranch Hand
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'll post some code in here. My normal disclaimer applies, Java 1.4, it's handjammed with no possibility of copy paste so there may be typos. A description of what I'm doing is populating fields of my GUI with an XML document. I want to be able to create/destroy nodes based on how the fields are filled out. What I'm looking to do is not allow the combo box to change until all the fields are filled out.

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Burton wrote:What I'm looking to do is not allow the combo box to change until all the fields are filled out.


Why not setEnabled(false) and use DocumentListeners to determine when to set that back to true?

It can be annoying to a user to find that the combo's list can be dropped down, the mouse can be clicked on an item in the list, and the item subsequently doesn't get selected. It can even be overlooked that the item didn't get selected, leading to even more confusion and irritation.

The usual idiom is to disable anything that shouldn't allow user interaction. Not to veto changes silently.
 
Charles Burton
Ranch Hand
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually had to look that one up, it looks like that is exactly what I need. I actually preferred to disable the combobox but wasn't sure how to go about it. Thanks again Daryll .
 
Charles Burton
Ranch Hand
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I misspelled your name, so Thank you again Darryl .
reply
    Bookmark Topic Watch Topic
  • New Topic