I´m trying to validate dynamically an item selected by a JComboBox, and I want to cancel the selection change in case the validation is not correct. Is there any way to achieve it?
private ItemListener itemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (true) CANCEL_CHANGE;
}
}
};
I tried to define a var holding the old value, unregistering listener, and select to previous state manually, but then the problem comes with the first change, because the var is not initialized and there´s no way hold the original value.
I also tried using ActionListener, but found no way to programtically cancel the change, and I don´t need fire event then there´s no change but I am assessing the chance of setSelection manually, so I revert to ItemListener.
This action listener gets the newly selected item from the combo box, uses it to compute the name of an image file, and updates a label to display the image. The combo box fires an action event when the user selects an item from the combo box's menu.
A JComboBox is a compact mechanism for selecting from a small number of options. The options are displayed when the user clicks on the combo box. If it is made editable the user can also enter text that is not supplied as one of the options.
u can make ur jcombobox uneditable by calling its setEnabled(false). A JComboBox is unEditable by default. You can make it editable with the setEditable(boolean) command. If you are talking about enabled or disabled you can set that by the setEnabled(boolean) method.
In the initial scenario when previous selection is not present just default it to the default selection index, like 0.
See the sample code below:
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestChangeListener {
final JTextField jTextField = new JTextField(20);
Object list[] = { "ItemA", "ItemB" };
int oldSelectionIndex = -1;
final JComboBox jComboBox = new JComboBox(list);
void init() {
JFrame jFrame = new JFrame("Test");
JPanel jPanel = new JPanel();
new TestChangeListener();
jPanel.add(jTextField);
jPanel.add(jComboBox);
jFrame.add(jPanel);
jComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
if (!"Okay".equalsIgnoreCase(jTextField.getText())) {
if (oldSelectionIndex < 0) {
jComboBox.setSelectedIndex(0);
} else {
jComboBox.setSelectedIndex(oldSelectionIndex);
}
} else {
oldSelectionIndex = jComboBox.getSelectedIndex();
}
}
}
});
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestChangeListener().init();
}
});
}
}
First time when textField doesnt contain any data, it just selects the default item, in this case its 0th element, you can have it your own. If data is present it checks and then decides if the current selection should be used or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With