Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a listener that listens to the enable/disable event in Swing?

Is there a listener I can add to a Swing combobox that will trigger when the combobox is enabled or disabled?

I have tried different listeners like componentlistener, itemlistener, propertychangelistener but in vain. I'm using JDK 1.6.

like image 317
BenMansourNizar Avatar asked Jan 11 '23 00:01

BenMansourNizar


1 Answers

PropertyChangeListener seems to work just fine for me...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class EnabledTest {

    public static void main(String[] args) {
        new EnabledTest();
    }

    public EnabledTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            final JComboBox cb = new JComboBox(new Object[]{"One", "Two", "Three"});
            add(cb, gbc);
            cb.addPropertyChangeListener("enabled", new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    System.out.println("State changed for " + evt.getPropertyName() + " to " + evt.getNewValue());
                }
            });

            JButton btn = new JButton("Switch");
            add(btn, gbc);
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    cb.setEnabled(!cb.isEnabled());
                }
            });
        }

    }

}
like image 60
MadProgrammer Avatar answered Jan 21 '23 17:01

MadProgrammer