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?
Joined: Jun 09, 2003
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.
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.
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.
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.
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.
There are no new questions, but there may be new answers.