Example usage for javax.swing.event ChangeListener ChangeListener

List of usage examples for javax.swing.event ChangeListener ChangeListener

Introduction

In this page you can find the example usage for javax.swing.event ChangeListener ChangeListener.

Prototype

ChangeListener

Source Link

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int minimum = 0;
    int maximum = 100;
    JProgressBar progress = new JProgressBar(minimum, maximum);

    progress.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            JProgressBar comp = (JProgressBar) evt.getSource();
            int value = comp.getValue();
            int min = comp.getMinimum();
            int max = comp.getMaximum();
        }//from   www  .  jav a2 s  . co m
    });
}

From source file:Main.java

public static void main(String[] args) {
    JButton button = new JButton("Test");
    ButtonModel model = button.getModel();
    model.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println("Armed: " + model.isArmed() + " Enabled: " + model.isEnabled() + " Pressed: "
                    + model.isPressed());
        }//w w w.jav a 2 s .c  om
    });

    JOptionPane.showMessageDialog(null, button);
}

From source file:AddingChangeListenerToJSpinner.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JSpinner dateSpinner = new JSpinner();

    dateSpinner.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            SpinnerModel dateModel = dateSpinner.getModel();
            System.out.println(dateModel.getValue());
        }/*from  w ww  .  jav a  2s  . c  o  m*/
    });
    frame.add(dateSpinner, "North");

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:RegisterChangeListenerToJSlider.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 10, 100, 20);
    framesPerSecond.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            if (!source.getValueIsAdjusting()) {
                int fps = (int) source.getValue();
                System.out.println(fps);
            }/* w  ww.ja v  a  2s .  c  om*/
        }
    });

    frame.add(framesPerSecond, "North");

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("JSpinner Dates");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ChangeListener listener = new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println("Source: " + e.getSource());
        }//from  w ww . java2 s . co m
    };

    SpinnerModel model3 = new SpinnerNumberModel();
    JSpinner spinner3 = new JSpinner(model3);
    spinner3.addChangeListener(listener);

    ChangeListener[] changeListeners = spinner3.getChangeListeners();

    frame.add(spinner3, BorderLayout.SOUTH);

    frame.setSize(200, 90);
    frame.setVisible(true);

}

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("JSpinner Dates");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ChangeListener listener = new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println("Source: " + e.getSource());
        }// w  w w  .  ja v  a 2s  . com
    };

    SpinnerModel model3 = new SpinnerNumberModel();
    JSpinner spinner3 = new JSpinner(model3);
    spinner3.addChangeListener(listener);

    frame.add(spinner3, BorderLayout.SOUTH);

    frame.setSize(200, 90);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider slider = new JSlider(0, 150, 0);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    slider.setPreferredSize(new Dimension(150, 30));
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent event) {
            int value = slider.getValue();
            if (value == 0) {
                System.out.println("0");
            } else if (value > 0 && value <= 30) {
                System.out.println("value > 0 && value <= 30");
            } else if (value > 30 && value < 80) {
                System.out.println("value > 30 && value < 80");
            } else {
                System.out.println("max");
            }//from w  w  w  . j  a va2s .c o  m
        }
    });
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTabbedPane tab = new JTabbedPane();
    tab.addTab("A", new JPanel());
    tab.addTab("+", new JPanel());
    tab.getModel().addChangeListener(new ChangeListener() {
        private int lastSelected;
        private boolean ignore = false;

        @Override/*from   w  ww.  j  a v  a  2s .c  o  m*/
        public void stateChanged(ChangeEvent e) {
            if (!ignore) {
                ignore = true;
                try {
                    int selected = tab.getSelectedIndex();
                    String title = tab.getTitleAt(selected);
                    if ("+".equals(title)) {
                        JPanel pane = new JPanel();
                        tab.insertTab("Tab" + (tab.getTabCount() - 1), null, pane, null, lastSelected + 1);
                        tab.setSelectedComponent(pane);
                    } else {
                        lastSelected = selected;
                    }
                } finally {
                    ignore = false;
                }
            }
        }
    });
    final JButton btn = new JButton("Add");
    btn.addActionListener(e -> System.out.println(tab.getTabCount()));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(tab);
    frame.add(btn, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Selecting Toggle");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToggleButton toggleButton = new JToggleButton("Toggle Button");

    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }//from  w w w  . j a  va  2  s  .c o  m
    };

    // Attach Listeners
    toggleButton.addChangeListener(changeListener);
    frame.add(toggleButton, BorderLayout.NORTH);
    frame.setSize(300, 125);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) {
    JButton jb = new JButton("Press Me");

    jb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            System.out.println("ItemEvent!");
        }/*  w ww . j a va  2  s  .co  m*/
    });

    jb.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent ev) {
            System.out.println("ChangeEvent!");
        }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}