Jcombobox Selection Change Listener

JComboBox Selection Change Listener?

It should respond to ActionListeners, like this:

combo.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
doSomething();
}
});

@John Calsbeek rightly points out that addItemListener() will work, too. You may get 2 ItemEvents, though, one for the deselection of the previously selected item, and another for the selection of the new item. Just don't use both event types!

JComboBox Selection Change

You are not accounting for the case where there is no selection.

public void setDetails()
{
Customer selected = (Customer) customerCombo.getSelectedItem();
if (selected != null)
{
// there is a selection so use it
}
else
{
// for example, clear the text boxes
}
}

We would also expect that changing the contents of the combo box might change its selection so we shouldn't ignore it.

How to add listener so that when a combobox value is changed

Rather then adding an ItemListener I would simply add an ActionListener which will be triggered every time the selected value is changed. Then you are able to just use comboBox.getSelectedItem() like so:

JComboBox comboBox_1; //you need to declare the comboBox and textField before the ActionListener.
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String[] v = a.getStudentID(comboBox.getSelectedItem().toString());
comboBox_1.setModel(new DefaultComboBoxModel<String>(v));

String y = a.getStudentName(comboBox_1.getSelectedItem().toString());
textField_3.setText(y);
}
});

Add you could extend this to change the values of your ComboBox or TextField within the actionPerformed method.

I think this is what you mean, though I may be wrong in the intent of your ActionListener.

Canceling selection change on a JComboBox if condition is satisfied (e.g validation on the incoming selection)

I can do that, dunno why you cannot do it. Have a look at this code example, select three times any values, then on the fourth time it will revert back to empty string :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboTest {

private JLabel imageLabel;
private JComboBox comboImage;

private String[] names = {"", "ukIcon","caIcon","unknwon"};
private boolean flag;
private int counter;

public ComboTest(){
flag = false;
counter = 0;
initComponents();
}

public void initComponents(){
JFrame frame = new JFrame("Test Combo");
frame.setSize(320, 160);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());

comboImage = new JComboBox(names);
comboImage.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event){
if(event.getStateChange() == ItemEvent.SELECTED){
if (flag)
comboImage.setSelectedItem("");
else
{
counter++;
if (counter == 3)
flag = true;
System.out.println((String) comboImage.getSelectedItem());
}
}
}
});

frame.add(comboImage);
frame.setVisible(true);
}

public static void main(String... args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ComboTest();
}
});
}
}

Code with Previous Value

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboTest {

private JLabel imageLabel;
private JComboBox comboImage;

private String[] names = {"", "ukIcon","caIcon","unknwon"};
private boolean flag;
private int counter;
private String previousValue;

public ComboTest(){
flag = false;
counter = 0;
previousValue = "";
initComponents();
}

public void initComponents(){
JFrame frame = new JFrame("Test Combo");
frame.setSize(320, 160);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());

comboImage = new JComboBox(names);
comboImage.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event){
if(event.getStateChange() == ItemEvent.SELECTED){
if (flag)
comboImage.setSelectedItem(previousValue);
else
{
counter++;
if (counter == 3)
flag = true;
previousValue = (String) comboImage.getSelectedItem();
System.out.println((String) comboImage.getSelectedItem());
}
}
}
});

frame.add(comboImage);
frame.setVisible(true);
}

public static void main(String... args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ComboTest();
}
});
}
}

Swing Dynamical JCombobox detect selection change by user

No, not really.

A possible solution is to disable event notification while the combo box is updated. This can be done in (at least) one of two ways...

Firstly, you could physically remove the listener from the combo box, if you have a reference to it.

Secondly, you set a boolean flag, which when true, the listener would ignore the event.

For example...

JComboBox, trigger an action if the selected item is change or the same is selected

Just add an ActionListener instead of an ItemListener.

see also JCombobox - Only execute actionlistener when value changes where someone had the opposite problem from you



Related Topics



Leave a reply



Submit