• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Veto Change in JComboBox

 
Charles Burton
Ranch Hand
Posts: 39
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.

 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I misspelled your name, so Thank you again Darryl .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic