List of usage examples for javax.swing JCheckBox addActionListener
public void addActionListener(ActionListener l)
ActionListener
to the button. 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/*from ww w . jav a 2s. c o m*/ checkBox.setEnabled(true); } }); JOptionPane.showMessageDialog(null, checkBox); }
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 va2 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:EditorDropTarget2.java
public static void main(String[] args) { try {/*from ww w . j ava 2s .c o m*/ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } final JFrame f = new JFrame("JEditor Pane Drop Target Example 2"); final JEditorPane pane = new JEditorPane(); // Add a drop target to the JEditorPane EditorDropTarget2 target = new EditorDropTarget2(pane); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); JPanel panel = new JPanel(); final JCheckBox editable = new JCheckBox("Editable"); editable.setSelected(true); panel.add(editable); editable.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { pane.setEditable(editable.isSelected()); } }); f.getContentPane().add(new JScrollPane(pane), BorderLayout.CENTER); f.getContentPane().add(panel, BorderLayout.SOUTH); f.setSize(500, 400); f.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 .j a va 2 s.c om*/ }; aCheckBox4.addActionListener(actionListener); frame.add(aCheckBox4); frame.setSize(300, 200); frame.setVisible(true); }
From source file:EditorDropTarget3.java
public static void main(String[] args) { try {//from w ww.j a va 2s .co m UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } final JFrame f = new JFrame("JEditor Pane Drop Target Example 3"); final JEditorPane pane = new JEditorPane(); // Add a drop target to the JEditorPane EditorDropTarget3 target = new EditorDropTarget3(pane); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); JPanel panel = new JPanel(); final JCheckBox editable = new JCheckBox("Editable"); editable.setSelected(true); panel.add(editable); editable.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { pane.setEditable(editable.isSelected()); } }); final JCheckBox enabled = new JCheckBox("Enabled"); enabled.setSelected(true); panel.add(enabled); enabled.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { pane.setEnabled(enabled.isSelected()); } }); f.getContentPane().add(new JScrollPane(pane), BorderLayout.CENTER); f.getContentPane().add(panel, BorderLayout.SOUTH); f.setSize(500, 400); f.setVisible(true); }
From source file:EditorDropTarget4.java
public static void main(String[] args) { try {/*from w ww . j a va 2 s . c o m*/ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } final JFrame f = new JFrame("JEditor Pane Drop Target Example 4"); // Create an editor with autoscrolling support final JEditorPane pane = new AutoScrollingEditorPane(); // Add a drop target to the JEditorPane EditorDropTarget4 target = new EditorDropTarget4(pane); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); JPanel panel = new JPanel(); final JCheckBox editable = new JCheckBox("Editable"); editable.setSelected(true); panel.add(editable); editable.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { pane.setEditable(editable.isSelected()); } }); final JCheckBox enabled = new JCheckBox("Enabled"); enabled.setSelected(true); panel.add(enabled); enabled.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { pane.setEnabled(enabled.isSelected()); } }); f.getContentPane().add(new JScrollPane(pane), BorderLayout.CENTER); f.getContentPane().add(panel, BorderLayout.SOUTH); f.setSize(500, 400); 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 w w w . j ava2 s . co m 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:DragDropTreeExample.java
public static void main(String[] args) { try {//from w w w .j av a 2 s .co m UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } final JFrame f = new JFrame("FileTree Drop Target Example"); try { final FileTree tree = new FileTree("D:\\"); // Add a drop target to the FileTree FileTreeDropTarget target = new FileTreeDropTarget(tree); tree.setEditable(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); JPanel panel = new JPanel(); final JCheckBox editable = new JCheckBox("Editable"); editable.setSelected(true); panel.add(editable); editable.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { tree.setEditable(editable.isSelected()); } }); final JCheckBox enabled = new JCheckBox("Enabled"); enabled.setSelected(true); panel.add(enabled); enabled.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { tree.setEnabled(enabled.isSelected()); } }); f.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER); f.getContentPane().add(panel, BorderLayout.SOUTH); f.setSize(500, 400); f.setVisible(true); } catch (Exception e) { System.out.println("Failed to build GUI: " + e); } }
From source file:DragDropTreeExample.java
public static void main(String[] args) { try {/*from w w w .ja va2 s . com*/ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } final JFrame f = new JFrame("FileTree Drop and Drop Example"); try { final FileTree tree = new FileTree("D:\\"); // Add a drop target to the FileTree FileTreeDropTarget target = new FileTreeDropTarget(tree); // Add a drag source to the FileTree FileTreeDragSource source = new FileTreeDragSource(tree); tree.setEditable(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); JPanel panel = new JPanel(); final JCheckBox editable = new JCheckBox("Editable"); editable.setSelected(true); panel.add(editable); editable.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { tree.setEditable(editable.isSelected()); } }); final JCheckBox enabled = new JCheckBox("Enabled"); enabled.setSelected(true); panel.add(enabled); enabled.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { tree.setEnabled(enabled.isSelected()); } }); f.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER); f.getContentPane().add(panel, BorderLayout.SOUTH); f.setSize(500, 400); f.setVisible(true); } catch (Exception e) { System.out.println("Failed to build GUI: " + e); } }
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 .j av 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); }