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(String text, Icon icon) 

Source Link

Document

Creates an initially unselected check box with the specified text and icon.

Usage

From source file:Main.java

public static void main(String[] args) {
    JCheckBox checkBox = new JCheckBox("Enabled", true);

    checkBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (checkBox.isEnabled())
                checkBox.setEnabled(false);
            else//  w  ww. j  a  v  a  2  s  .  c  o  m
                checkBox.setEnabled(true);
        }
    });
    JOptionPane.showMessageDialog(null, checkBox);
}

From source file:UsingComponentListener.java

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

    JCheckBox checkbox = new JCheckBox("Label visible", true);
    checkbox.addComponentListener(new ComponentListener() {
        public void componentHidden(ComponentEvent e) {
            System.out.println("componentHidden event from " + e.getComponent().getClass().getName());
        }//from  w w w  .  ja  v  a2  s .  c o m

        public void componentMoved(ComponentEvent e) {
            Component c = e.getComponent();
            System.out.println("componentMoved event from " + c.getClass().getName() + "; new location: "
                    + c.getLocation().x + ", " + c.getLocation().y);
        }

        public void componentResized(ComponentEvent e) {
            Component c = e.getComponent();
            System.out.println("componentResized event from " + c.getClass().getName() + "; new size: "
                    + c.getSize().width + ", " + c.getSize().height);
        }

        public void componentShown(ComponentEvent e) {
            System.out.println("componentShown event from " + e.getComponent().getClass().getName());
        }

    });

    frame.add(checkbox, "North");

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

From source file:Main.java

public static void main(String[] args) {
    final JButton ok = new JButton("ok");
    JCheckBox cb = new JCheckBox("Enabled", true);
    cb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (ok.isEnabled())
                ok.setEnabled(false);//  w  w  w .  j  a v  a2  s .  c  o  m
            else
                ok.setEnabled(true);
        }
    });

    ButtonModel model = new DefaultButtonModel() {
        public void setEnabled(boolean b) {
            if (b)
                System.out.println("Pressed: true");
            else
                System.out.println("Pressed: false");

            super.setEnabled(b);
        }

        public void setArmed(boolean b) {
            if (b)
                System.out.println("Armed: true");
            else
                System.out.println("Armed: false");

            super.setArmed(b);
        }

        public void setPressed(boolean b) {
            if (b)
                System.out.println("Pressed: true");
            else
                System.out.println("Pressed: false");

            super.setPressed(b);
        }

    };

    ok.setModel(model);

    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(ok);
    f.add(cb);

    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:ButtonModel.java

public static void main(String[] args) {
    final JButton ok = new JButton("ok");

    JCheckBox cb = new JCheckBox("Enabled", true);

    ok.setBounds(40, 30, 80, 25);/*from   www.j av  a 2  s  .  c  om*/
    ok.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            DefaultButtonModel model = (DefaultButtonModel) ok.getModel();
            if (model.isEnabled())
                System.out.println("Enabled: true");
            else
                System.out.println("Enabled: false");

            if (model.isArmed())
                System.out.println("Armed: true");
            else
                System.out.println("Armed: false");

            if (model.isPressed())
                System.out.println("Pressed: true");
            else
                System.out.println("Pressed: false");
        }

    });

    cb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (ok.isEnabled())
                ok.setEnabled(false);
            else
                ok.setEnabled(true);
        }
    });

    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(ok);
    f.add(cb);
    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

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());
        }/*from w  ww  . j av  a2s . com*/
    });

    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:CheckBoxIcon.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon checked = new CheckBoxIcon();
    Icon unchecked = new CheckBoxIcon();
    JCheckBox aCheckBox1 = new JCheckBox("Pizza", unchecked);
    aCheckBox1.setSelectedIcon(checked);
    JCheckBox aCheckBox2 = new JCheckBox("Calzone");
    aCheckBox2.setIcon(unchecked);/*from   w  w  w . j av a 2s .  com*/
    aCheckBox2.setSelectedIcon(checked);
    Icon checkBoxIcon = new CheckBoxIcon();
    JCheckBox aCheckBox3 = new JCheckBox("Anchovies", checkBoxIcon);
    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust", checked);
    frame.setLayout(new GridLayout(0, 1));
    frame.add(aCheckBox1);
    frame.add(aCheckBox2);
    frame.add(aCheckBox3);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:CheckBoxIcon.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Iconizing CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon checked = new CheckBoxIcon();
    Icon unchecked = new CheckBoxIcon();
    JCheckBox aCheckBox1 = new JCheckBox("Pizza", unchecked);
    aCheckBox1.setSelectedIcon(checked);
    JCheckBox aCheckBox2 = new JCheckBox("Calzone");
    aCheckBox2.setIcon(unchecked);//from w ww.  j  a v  a2s.c  o m
    aCheckBox2.setSelectedIcon(checked);
    Icon checkBoxIcon = new CheckBoxIcon();
    JCheckBox aCheckBox3 = new JCheckBox("Anchovies", checkBoxIcon);
    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust", checked);
    frame.setLayout(new GridLayout(0, 1));
    frame.add(aCheckBox1);
    frame.add(aCheckBox2);
    frame.add(aCheckBox3);
    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();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel gui = new JPanel(new BorderLayout(5, 5));

    JPanel plafComponents = new JPanel(new FlowLayout(FlowLayout.RIGHT, 3, 3));
    plafComponents.setBorder(new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)"));

    UIManager.LookAndFeelInfo[] plafInfos = UIManager.getInstalledLookAndFeels();
    String[] plafNames = new String[plafInfos.length];
    for (int ii = 0; ii < plafInfos.length; ii++) {
        plafNames[ii] = plafInfos[ii].getName();
    }//from  w  w w  .j a  v  a 2  s.com
    JComboBox plafChooser = new JComboBox(plafNames);
    plafComponents.add(plafChooser);

    JCheckBox pack = new JCheckBox("Pack on PLAF change", true);
    plafComponents.add(pack);

    plafChooser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            int index = plafChooser.getSelectedIndex();
            try {
                UIManager.setLookAndFeel(plafInfos[index].getClassName());
                SwingUtilities.updateComponentTreeUI(frame);
                if (pack.isSelected()) {
                    frame.pack();
                    frame.setMinimumSize(frame.getSize());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    gui.add(plafComponents, BorderLayout.NORTH);

    JPanel dynamicLabels = new JPanel(new BorderLayout(4, 4));
    dynamicLabels.setBorder(new TitledBorder("BorderLayout(4,4)"));
    gui.add(dynamicLabels, BorderLayout.WEST);

    final JPanel labels = new JPanel(new GridLayout(0, 2, 3, 3));
    labels.setBorder(new TitledBorder("GridLayout(0,2,3,3)"));

    JButton addNew = new JButton("Add Another Label");
    dynamicLabels.add(addNew, BorderLayout.NORTH);
    addNew.addActionListener(new ActionListener() {

        private int labelCount = 0;

        public void actionPerformed(ActionEvent ae) {
            labels.add(new JLabel("Label " + ++labelCount));
            frame.validate();
        }
    });

    dynamicLabels.add(new JScrollPane(labels), BorderLayout.CENTER);

    String[] header = { "Name", "Value" };
    String[] a = new String[0];
    String[] names = System.getProperties().stringPropertyNames().toArray(a);
    String[][] data = new String[names.length][2];
    for (int ii = 0; ii < names.length; ii++) {
        data[ii][0] = names[ii];
        data[ii][1] = System.getProperty(names[ii]);
    }
    DefaultTableModel model = new DefaultTableModel(data, header);
    JTable table = new JTable(model);

    JScrollPane tableScroll = new JScrollPane(table);
    Dimension tablePreferred = tableScroll.getPreferredSize();
    tableScroll.setPreferredSize(new Dimension(tablePreferred.width, tablePreferred.height / 3));

    JPanel imagePanel = new JPanel(new GridBagLayout());
    JLabel imageLabel = new JLabel("test");
    imagePanel.add(imageLabel, null);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tableScroll, new JScrollPane(imagePanel));
    gui.add(splitPane, BorderLayout.CENTER);

    frame.setContentPane(gui);
    frame.pack();
    frame.setVisible(true);
}

From source file:IconCheckBoxSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Iconizing CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon checked = new DiamondIcon(Color.black, true);
    Icon unchecked = new DiamondIcon(Color.black, false);
    JCheckBox aCheckBox1 = new JCheckBox("Pizza", unchecked);
    aCheckBox1.setSelectedIcon(checked);
    JCheckBox aCheckBox2 = new JCheckBox("Calzone");
    aCheckBox2.setIcon(unchecked);/*  w w  w . j  ava  2 s.  c  om*/
    aCheckBox2.setSelectedIcon(checked);
    Icon checkBoxIcon = new CheckBoxIcon();
    JCheckBox aCheckBox3 = new JCheckBox("Anchovies", checkBoxIcon);
    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust", checked);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));
    contentPane.add(aCheckBox1);
    contentPane.add(aCheckBox2);
    contentPane.add(aCheckBox3);
    contentPane.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void showFrameWithToolBar(String toolBarPosition) {

    JPanel gui = new JPanel(new BorderLayout());

    JToolBar tb = new JToolBar();

    gui.add(tb, toolBarPosition);/*from  w w w  .j  a v a2  s . c om*/
    tb.add(new JButton("Button 1"));
    tb.add(new JButton("Button 2"));
    tb.addSeparator();
    tb.add(new JButton("Button 3"));
    tb.add(new JCheckBox("Check 1", true));

    JFrame f = new JFrame(toolBarPosition);
    f.setContentPane(gui);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationByPlatform(true);
    f.pack();

    f.setSize(400, 120);
    f.setVisible(true);
}