Example usage for javax.swing JCheckBox addItemListener

List of usage examples for javax.swing JCheckBox addItemListener

Introduction

In this page you can find the example usage for javax.swing JCheckBox addItemListener.

Prototype

public void addItemListener(ItemListener l) 

Source Link

Document

Adds an ItemListener to the checkbox.

Usage

From source file:Main.java

public static void main(String args[]) {
    String ITEMS[] = { "Black", "Blue", "Green", "Orange", "Purple", "Red", "White" };
    JList jList = new JList(ITEMS);
    jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scroll = new JScrollPane(jList);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    JCheckBox chkEnable = new JCheckBox("Enable", true);
    chkEnable.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            jList.setEnabled(chkEnable.isSelected());
        }//w w  w .  j  a va 2s  . c  o m
    });

    JFrame f = new JFrame("Colors");
    Container contentPane = f.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(scroll, BorderLayout.CENTER);
    contentPane.add(chkEnable, BorderLayout.NORTH);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(180, 220);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame result = new JFrame();
    JComboBox<String> combobox = new JComboBox<>(new String[] { "foo", "bar", "aaa", "Hello World" });
    result.add(combobox, BorderLayout.CENTER);

    JCheckBox toggleVisibility = new JCheckBox("Toggle visibility");
    toggleVisibility.setSelected(combobox.isVisible());
    toggleVisibility.addItemListener(e -> {
        combobox.setVisible(e.getStateChange() == ItemEvent.SELECTED);
    });//from www.  j  a  v a2 s . c o m
    result.add(toggleVisibility, BorderLayout.SOUTH);

    result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    result.pack();
    result.setVisible(true);

}

From source file:Main.java

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

    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust");

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
            Color foreground = abstractButton.getForeground();
            Color background = abstractButton.getBackground();
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                abstractButton.setForeground(background);
                abstractButton.setBackground(foreground);
            }/*from   w w w  . j  a  v a2  s.  co m*/
        }
    };
    aCheckBox4.addItemListener(itemListener);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {
    final String DESELECTED_LABEL = "Deselected";
    final String SELECTED_LABEL = "Selected";

    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL);
            abstractButton.setText(newLabel);
        }// w ww . j  a v a2  s  .  c o m
    };

    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);
        }
    };

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
            Color foreground = abstractButton.getForeground();
            Color background = abstractButton.getBackground();
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                abstractButton.setForeground(background);
                abstractButton.setBackground(foreground);
            }
        }
    };

    checkBox.addActionListener(actionListener);
    checkBox.addChangeListener(changeListener);
    checkBox.addItemListener(itemListener);
    checkBox.setMnemonic(KeyEvent.VK_S);
    frame.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:SelectingCheckBox.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL);
    // Define ActionListener
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL);
            abstractButton.setText(newLabel);
        }/*from w  w w .  jav  a 2 s.  co m*/
    };
    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);
        }
    };

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
            Color foreground = abstractButton.getForeground();
            Color background = abstractButton.getBackground();
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                abstractButton.setForeground(background);
                abstractButton.setBackground(foreground);
            }
        }
    };

    checkBox.addActionListener(actionListener);
    checkBox.addChangeListener(changeListener);
    checkBox.addItemListener(itemListener);
    checkBox.setMnemonic(KeyEvent.VK_S);
    Container contentPane = frame.getContentPane();
    contentPane.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    JCheckBox cb = new JCheckBox("www.java2s.com");
    cb.addItemListener(this);
    add(cb);
}

From source file:ToggleButtonCheckBoxRadioButton.java

public TogglePanel() {
    JToggleButton tog = new JToggleButton("Toggle");
    ItemListener listener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            AbstractButton src = (AbstractButton) (e.getSource());
            System.out.println("Toggle: " + src.getText());
        }//from w  w w .ja  v a 2  s.co  m
    };
    tog.addItemListener(listener);
    add(tog);

    JCheckBox cbox = new JCheckBox("Checkbox");
    cbox.addItemListener(listener);
    add(cbox);
    ButtonGroup btngroup = new ButtonGroup();
    for (int i = 1; i <= 3; i++) {
        JRadioButton radio = new JRadioButton("Radio " + i);
        btngroup.add(radio);
        radio.addItemListener(listener);
        add(radio);
    }
}

From source file:components.GlassPaneDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from  w  ww  . j  a  v a  2 s.  c o  m*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("GlassPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Start creating and adding components.
    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);

    //Set up the content pane, where the "main GUI" lives.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(changeButton);
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    //Set up the menu bar, which appears above the content pane.
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.add(new JMenuItem("Do nothing"));
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    //Set up the glass pane, which appears over both menu bar
    //and content pane and is an item listener on the change
    //button.
    myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane());
    changeButton.addItemListener(myGlassPane);
    frame.setGlassPane(myGlassPane);

    //Show the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:GlassPaneDemo.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 */// w  w w. j ava2  s. co m
private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("GlassPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Start creating and adding components.
    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);

    // Set up the content pane, where the "main GUI" lives.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(changeButton);
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    // Set up the menu bar, which appears above the content pane.
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.add(new JMenuItem("Do nothing"));
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    // Set up the glass pane, which appears over both menu bar
    // and content pane and is an item listener on the change
    // button.
    myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane());
    changeButton.addItemListener(myGlassPane);
    frame.setGlassPane(myGlassPane);

    // Show the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    // Add check boxes to the content pane.
    Icon normal = new MyIcon(Color.red);
    Icon rollover = new MyIcon(Color.YELLOW);
    Icon selected = new MyIcon(Color.BLUE);

    JCheckBox cb = new JCheckBox();
    cb.setRolloverIcon(rollover);/*from ww  w  . j  a  v a2 s . c  o m*/
    cb.setSelectedIcon(selected);
    cb.addItemListener(this);
    add(cb);

}