List of usage examples for javax.swing JCheckBox JCheckBox
public JCheckBox(Action a)
From source file:Main.java
public static void main(String[] argv) throws Exception { Action action = new AbstractAction("CheckBox Label") { // called when the button is pressed public void actionPerformed(ActionEvent evt) { JCheckBox cb = (JCheckBox) evt.getSource(); // Determine status boolean isSel = cb.isSelected(); if (isSel) { // selected } else { // deselected }//from ww w . j a v a2 s.c o m } }; // Create the checkbox from the action JCheckBox checkBox = new JCheckBox(action); }
From source file:Main.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); }// ww w . jav a 2 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.getModel().addActionListener(actionListener); checkBox.getModel().addChangeListener(changeListener); checkBox.getModel().addItemListener(itemListener); frame.add(checkBox, BorderLayout.NORTH); frame.setSize(300, 100); frame.setVisible(true); }
From source file:Main.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); }/*from w ww. ja v a 2 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.getModel().addActionListener(actionListener); checkBox.getModel().addChangeListener(changeListener); checkBox.getModel().addItemListener(itemListener); checkBox.getModel().removeActionListener(actionListener); checkBox.getModel().removeChangeListener(changeListener); checkBox.getModel().removeItemListener(itemListener); frame.add(checkBox, BorderLayout.NORTH); frame.setSize(300, 100); 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); }/*from w w w. ja v a 2 s . c om*/ }; 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 . j ava 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 static void main(String[] args) { String[] data = { "alist", "arithmetic", "ASCIInumbers", "A", "B", "list", "C", "D", "E", "numeral", "G", "F" }; JCheckBox[] checkBox;//from w w w . j a v a 2s .c om JButton submitButton; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(5, 5)); checkBox = new JCheckBox[data.length]; JPanel centerPanel = new JPanel(); centerPanel.setLayout(new GridLayout(0, 4, 5, 5)); for (int i = 0; i < data.length; i++) { checkBox[i] = new JCheckBox(data[i]); centerPanel.add(checkBox[i]); } contentPane.add(centerPanel, BorderLayout.CENTER); JPanel footerPanel = new JPanel(); submitButton = new JButton("Submit"); footerPanel.add(submitButton); contentPane.add(footerPanel, BorderLayout.PAGE_END); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JPanel panel = new JPanel(new GridLayout(0, 1)); Border border = BorderFactory.createTitledBorder("Examples"); panel.setBorder(border);//from w ww . j av a 2s. c o m ButtonGroup group = new ButtonGroup(); AbstractButton abstract1 = new JToggleButton("Toggle Button"); panel.add(abstract1); group.add(abstract1); AbstractButton abstract2 = new JRadioButton("Radio Button"); panel.add(abstract2); group.add(abstract2); AbstractButton abstract3 = new JCheckBox("Check Box"); panel.add(abstract3); group.add(abstract3); AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item"); panel.add(abstract4); group.add(abstract4); AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item"); panel.add(abstract5); group.add(abstract5); JFrame frame = new JFrame("Button Group"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.add(panel, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
From source file:TryBoxLayout.java
public static void main(String[] args) { JFrame aWindow = new JFrame("This is a Box Layout"); aWindow.setBounds(30, 30, 300, 300); aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create left column of radio buttons Box left = Box.createVerticalBox(); ButtonGroup radioGroup = new ButtonGroup(); JRadioButton rbutton;/* ww w.ja va2s . c om*/ radioGroup.add(rbutton = new JRadioButton("Red")); left.add(rbutton); radioGroup.add(rbutton = new JRadioButton("Green")); left.add(rbutton); radioGroup.add(rbutton = new JRadioButton("Blue")); left.add(rbutton); radioGroup.add(rbutton = new JRadioButton("Yellow")); left.add(rbutton); Box right = Box.createVerticalBox(); right.add(new JCheckBox("A")); right.add(new JCheckBox("B")); right.add(new JCheckBox("C")); Box top = Box.createHorizontalBox(); top.add(left); top.add(right); Container content = aWindow.getContentPane(); content.setLayout(new BorderLayout()); content.add(top, BorderLayout.CENTER); aWindow.pack(); aWindow.setVisible(true); }
From source file:DiamondAbstractButtonStateIcon.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox bn = new JCheckBox("Click to change the icon color"); bn.setIcon(new DiamondAbstractButtonStateIcon()); frame.add(bn);//from w w w .j a v a2 s. c o m frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JPanel entreePanel = new JPanel(); final ButtonGroup entreeGroup = new ButtonGroup(); JRadioButton radioButton;//from w w w .j a v a 2s . co m entreePanel.add(radioButton = new JRadioButton("A")); radioButton.setActionCommand("A"); entreeGroup.add(radioButton); entreePanel.add(radioButton = new JRadioButton("B")); radioButton.setActionCommand("B"); entreeGroup.add(radioButton); entreePanel.add(radioButton = new JRadioButton("C", true)); radioButton.setActionCommand("C"); entreeGroup.add(radioButton); final JPanel condimentsPanel = new JPanel(); condimentsPanel.add(new JCheckBox("Ketchup")); condimentsPanel.add(new JCheckBox("Mustard")); condimentsPanel.add(new JCheckBox("Pickles")); JPanel orderPanel = new JPanel(); JButton orderButton = new JButton("Place Order"); orderPanel.add(orderButton); frame.setLayout(new GridLayout(3, 1)); frame.add(entreePanel); frame.add(condimentsPanel); frame.add(orderPanel); orderButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { String entree = entreeGroup.getSelection().getActionCommand(); System.out.println(entree + " sandwich"); Component[] components = condimentsPanel.getComponents(); for (Component c : components) { JCheckBox cb = (JCheckBox) c; if (cb.isSelected()) System.out.println("With " + cb.getText()); } } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 150); frame.setVisible(true); }