List of usage examples for javax.swing ButtonModel isArmed
boolean isArmed();
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 ww . j a 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.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: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); }// w ww. j a v a 2s . 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:Main.java
public static void main(String[] args) { JButton button = new JButton("Test"); ButtonModel model = button.getModel(); model.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { System.out.println("Armed: " + model.isArmed() + " Enabled: " + model.isEnabled() + " Pressed: " + model.isPressed()); }/*ww w . j a va 2 s. c o m*/ }); JOptionPane.showMessageDialog(null, button); }
From source file:GroupActionRadio.java
public static void main(String args[]) { String title = (args.length == 0 ? "Grouping Example" : args[0]); JFrame frame = new JFrame(title); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Slice Parts ActionListener sliceActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton aButton = (AbstractButton) actionEvent.getSource(); System.out.println("Selected: " + aButton.getText()); }//from www .j a v a2 s.c om }; Container sliceContainer = RadioButtonUtils.createRadioButtonGrouping(sliceOptions, "Slice Count", sliceActionListener); // Crust Parts ActionListener crustActionListener = new ActionListener() { String lastSelected; public void actionPerformed(ActionEvent actionEvent) { AbstractButton aButton = (AbstractButton) actionEvent.getSource(); String label = aButton.getText(); String msgStart; if (label.equals(lastSelected)) { msgStart = "Reselected: "; } else { msgStart = "Selected: "; } lastSelected = label; System.out.println(msgStart + label); } }; ItemListener itemListener = new ItemListener() { String lastSelected; public void itemStateChanged(ItemEvent itemEvent) { AbstractButton aButton = (AbstractButton) itemEvent.getSource(); int state = itemEvent.getStateChange(); String label = aButton.getText(); String msgStart; if (state == ItemEvent.SELECTED) { if (label.equals(lastSelected)) { msgStart = "Reselected -> "; } else { msgStart = "Selected -> "; } lastSelected = label; } else { msgStart = "Deselected -> "; } System.out.println(msgStart + label); } }; ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changEvent) { AbstractButton aButton = (AbstractButton) changEvent.getSource(); ButtonModel aModel = aButton.getModel(); boolean armed = aModel.isArmed(); boolean pressed = aModel.isPressed(); boolean selected = aModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); } }; final Container crustContainer = RadioButtonUtils.createRadioButtonGrouping(crustOptions, "Crust Type", crustActionListener, itemListener, changeListener); // Button Parts ActionListener buttonActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Enumeration selected = RadioButtonUtils.getSelectedElements(crustContainer); while (selected.hasMoreElements()) { System.out.println("Selected -> " + selected.nextElement()); } } }; JButton button = new JButton("Order Pizza"); button.addActionListener(buttonActionListener); Container contentPane = frame.getContentPane(); contentPane.add(sliceContainer, BorderLayout.WEST); contentPane.add(crustContainer, BorderLayout.EAST); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:JToggleButtonMenuItem.java
public void menuSelectionChanged(boolean isIncluded) { ButtonModel model = getModel(); // Only change armed state if different if (model.isArmed() != isIncluded) { model.setArmed(isIncluded);/*from ww w . ja v a 2 s. c o m*/ } if (isIncluded) { savedForeground = getForeground(); if (!savedForeground.equals(Color.BLUE)) { setForeground(Color.BLUE); } else { // In case foreground blue, use something different setForeground(Color.RED); } } else { setForeground(savedForeground); // If null, get foreground from installed look and feel if (savedForeground == null) { updateUI(); } } }
From source file:ToggleSample.java
public void menuSelectionChanged(boolean isIncluded) { ButtonModel model = getModel(); // only change armed state if different if (model.isArmed() != isIncluded) { model.setArmed(isIncluded);//from w ww .ja va2 s.co m } if (isIncluded) { savedForeground = getForeground(); if (!savedForeground.equals(Color.blue)) { setForeground(Color.blue); } else { // In case foreground blue, use something different setForeground(Color.red); } } else { setForeground(savedForeground); // if null, get foreground from installed look and feel if (savedForeground == null) { updateUI(); } } }