Is there some way to block changing a JComboBox when state=navigate?
Hi,
I have a database system in Java5 with many JComboBoxes.
The user has two states: navigate from a database record to another and, when it clicks Edit button, goes to edit state. But I don't want to use combo.setEnabled(false) because the text into JComboBox becomes hard to see.
I would like to know if there is some property like "combobox.setReadOnly(true)". If there was this property it would be very easy. Then, the users could try to change the item index, but none would occurs.
null
> I don't want to use combo.setEnabled(false)
> because the text into JComboBox becomes
> hard to see.
Here's how you can change the disabled foreground color of ALL JComboBoxes.
UIManager.put("ComboBox.disabledForeground", Color.BLACK);
More UI defaults -- http://www.alethis.net/reference/java/uidefaults.html
> I would like to know if there is some property like
> "combobox.setReadOnly(true)". If there was this
> property it would be very easy. Then, the users could
> try to change the item index, but none would occurs.
You'll have to fake it on your own. Off the top of my head, this is what I would try (completely untested of course):
1) Subclass your combo and add a "setReadOnly(boolean)" method .
2) Subclass DefaultComboBoxModel and override any method that changes the selection. The overridden method(s) should be blank.
3) When setReadOnly(true) is called on your subclassed combo, create an instance of a ReadOnlyComboBoxModel(step #2), supplying it with the current list of items as well as the selected item from the original model. When setReadOnly(false) is called on your subclassed combo, switch back to a default model with the info from the read-only model.
4) Also, if the combo was originally editable, grab a handle to the editor component, which is a JTextField, and call setEditable(false) on it, and back to true again if readOnly = false.
Instead of the read-only model approach, you could also try grabbing a handle to the down-arrow and either disabling it or removing the listeners from it.
I had to do the read-only model approach for JCheckBoxes in one of my apps, and it worked fine. So hopefully the idea can translate over to combos as well.
I agree that Swing fell a little short in this department. After all, they felt the need to have setEditable(boolean) for JTextComponents, so why not for all JComponents? I wrote an RFE to Sun on this very topic. They said the average response time was 3 weeks before I hear back from them. It's been 5 weeks with still no word. I'll post it here when I do hear something.
> Here's how you can change the disabled foreground color of ALL JComboBoxes.
> UIManager.put("ComboBox.disabledForeground", Color.BLACK);
With this approach, truly disabled combos don't look disabled anymore, which just reverses the problem.
Modifying the selection behaviour of these any of these selection controls the trick is always, subclass the selection model. The selection methods then either call, or refrain from calling, their super equivalents. e,g.
public void setSelectedItem(Object newSelection) {
if(!readOnly)
super.setSelectedItem(newSelection();
}
You can even add methods to the subclass calling the super methods to force selection. I've got selection models which load stuff from a database when you click on a tab before the tab actually changes.