File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Veto Change in JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Veto Change in JComboBox" Watch "Veto Change in JComboBox" New topic
Author

Veto Change in JComboBox

Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

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

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

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

Joined: Jan 31, 2011
Posts: 39

And I misspelled your name, so Thank you again Darryl .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Veto Change in JComboBox
 
Similar Threads
Combo Box for flight origin and destination
JComboBox Problem...Help
how to add dropdownlistbox to text field
How to restrict data in JTextField?
drop down menu