Example usage for javax.swing JCheckBox JCheckBox

List of usage examples for javax.swing JCheckBox JCheckBox

Introduction

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

Prototype

public JCheckBox(Action a) 

Source Link

Document

Creates a check box where properties are taken from the Action supplied.

Usage

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox("Hi");

    checkBox.getModel().setActionCommand("Hi");

    frame.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);/*  w w  w.j a v  a  2 s  .  c  o m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox("Hi");

    checkBox.getModel().setMnemonic((char) 'H');
    System.out.println(checkBox.getModel().getMnemonic());

    frame.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);//from   www  . j a va 2  s  .c  om
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox("Hi");

    checkBox.getModel().setEnabled(false);
    System.out.println(checkBox.getModel().isEnabled());

    frame.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);/*from  w w w.  j a v a 2 s . c  o m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox("Hi");

    checkBox.getModel().setEnabled(false);
    System.out.println(checkBox.getModel().isRollover());

    frame.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);//  ww  w .  ja va 2s  . c  om
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox("Hi");

    ButtonGroup bg = new ButtonGroup();
    checkBox.getModel().setGroup(bg);//from  w w  w.  j  a  v  a2  s.c  om

    frame.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:JCheckBoxActionListener.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");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println(selected);
            // abstractButton.setText(newLabel);
        }//from  w w w .  java  2s. c  o  m
    };
    aCheckBox4.addActionListener(actionListener);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.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   ww w  .j  a  v  a 2  s.  co  m
        }
    };
    aCheckBox4.addItemListener(itemListener);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:JCheckBoxTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JCheckBox Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox ac = new JCheckBox("A/C");
    ac.setSelected(true);/*from  w  ww.  jav a 2s. co m*/
    JCheckBox cdPlayer = new JCheckBox("A");
    JCheckBox cruiseControl = new JCheckBox("B");
    JCheckBox keylessEntry = new JCheckBox("C");
    JCheckBox antiTheft = new JCheckBox("D");
    JCheckBox centralLock = new JCheckBox("E");

    frame.add(new JLabel("Car Features"));
    frame.add(ac);
    frame.add(cdPlayer);
    frame.add(cruiseControl);
    frame.add(keylessEntry);
    frame.add(antiTheft);
    frame.add(centralLock);
    frame.pack();
    frame.setVisible(true);
}

From source file:JCheckBoxChangeListener.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");

    // Define ChangeListener
    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);
        }/*  w w w .  java 2 s. c  o  m*/
    };

    aCheckBox4.addChangeListener(changeListener);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.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);
    });/*w ww. j av  a2s.  co m*/
    result.add(toggleVisibility, BorderLayout.SOUTH);

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

}