List of usage examples for javax.swing JToggleButton JToggleButton
public JToggleButton(Action a)
From source file:JToggleButtonEventListenerSequence.java
public static void main(String args[]) { JFrame frame = new JFrame("Selecting Toggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Toggle Button"); // Define ActionListener ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); System.out.println("Action - selected=" + selected + "\n"); }// ww w .ja va2s .c o m }; // 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); } }; // Define ItemListener ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { System.out.println("Selected"); } else { System.out.println("Deselected"); } } }; // Attach Listeners toggleButton.addActionListener(actionListener); toggleButton.addChangeListener(changeListener); toggleButton.addItemListener(itemListener); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JToolBar toolbar = new JToolBar(); ImageIcon icon = new ImageIcon("icon.gif"); Action action = new AbstractAction("Button Label", icon) { public void actionPerformed(ActionEvent evt) { }/*from ww w. jav a2 s .co m*/ }; JButton c1 = new JButton(action); c1.setText(null); c1.setMargin(new Insets(0, 0, 0, 0)); toolbar.add(c1); JToggleButton c2 = new JToggleButton(action); c2.setText(null); c2.setMargin(new Insets(0, 0, 0, 0)); toolbar.add(c2); JComboBox c3 = new JComboBox(new String[] { "A", "B", "C" }); c3.setPrototypeDisplayValue("XXXXXXXX"); // Set a desired width c3.setMaximumSize(c3.getMinimumSize()); toolbar.add(c3); }
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 a v a 2 s.co 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:AButtonGroup.java
public static void main(String args[]) { JFrame frame = new JFrame("Button Group"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); Border border = BorderFactory.createTitledBorder("Examples"); panel.setBorder(border);/* w w w. ja v a 2s. co 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); frame.add(panel, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
From source file:AButtonGroup.java
public static void main(String args[]) { JFrame frame = new JFrame("Button Group"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); Border border = BorderFactory.createTitledBorder("Examples"); panel.setBorder(border);/*from w ww. j a v a 2 s . 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); Container contentPane = frame.getContentPane(); contentPane.add(panel, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
From source file:FindingAButtonGroup.java
public static void main(String args[]) { JFrame frame = new JFrame("Button Group"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); Border border = BorderFactory.createTitledBorder("Examples"); panel.setBorder(border);//from w ww. j a v a 2 s . co 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); frame.add(panel, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); group.setSelected(abstract1.getModel(), true); Enumeration elements = group.getElements(); while (elements.hasMoreElements()) { AbstractButton button = (AbstractButton) elements.nextElement(); if (button.isSelected()) { System.out.println("The winner is: " + button.getText()); } } }
From source file:SettingSelectedAButtonGroup.java
public static void main(String args[]) { JFrame frame = new JFrame("Button Group"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); Border border = BorderFactory.createTitledBorder("Examples"); panel.setBorder(border);/*from w w w. j a v a 2 s.co 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); frame.add(panel, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); group.setSelected(abstract1.getModel(), true); Enumeration elements = group.getElements(); while (elements.hasMoreElements()) { AbstractButton button = (AbstractButton) elements.nextElement(); if (button.isSelected()) { System.out.println("The winner is: " + button.getText()); } } }
From source file:ASelectedButton.java
public static void main(String args[]) { JFrame frame = new JFrame("Radio Buttons"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); Border border = BorderFactory.createTitledBorder("Options"); panel.setBorder(border);//from w w w. ja v a 2s.c o m final ButtonGroup group = new ButtonGroup(); AbstractButton abstract1 = new JRadioButton("One"); panel.add(abstract1); group.add(abstract1); AbstractButton abstract2 = new JRadioButton("Two"); panel.add(abstract2); group.add(abstract2); AbstractButton abstract3 = new JRadioButton("Three"); panel.add(abstract3); group.add(abstract3); AbstractButton abstract4 = new JRadioButton("Four"); panel.add(abstract4); group.add(abstract4); AbstractButton abstract5 = new JRadioButton("Five"); panel.add(abstract5); group.add(abstract5); ActionListener aListener = new ActionListener() { public void actionPerformed(ActionEvent event) { Enumeration elements = group.getElements(); while (elements.hasMoreElements()) { AbstractButton button = (AbstractButton) elements.nextElement(); if (button.isSelected()) { System.out.println("The winner is: " + button.getText()); break; } } group.setSelected(null, true); } }; JToggleButton button = new JToggleButton("Show Selected"); button.addActionListener(aListener); Container container = frame.getContentPane(); container.add(panel, BorderLayout.CENTER); container.add(button, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
/** Constructs a JToggleButton with an icon from the given file id. */ public static JToggleButton makeToggleButton(final Object owner, final String id, final String altText, final int wpad, final int hpad) { final URL url = owner.getClass().getResource(id); ImageIcon icon = null;//from ww w . j ava 2 s .c o m if (url != null) icon = new ImageIcon(url); JToggleButton button; if (icon == null) button = new JToggleButton(altText); else { button = new JToggleButton(icon); button.setPreferredSize(new Dimension(icon.getIconWidth() + wpad, icon.getIconHeight() + hpad)); } return button; }
From source file:MainClass.java
public MainClass() { JToggleButton tog = new JToggleButton("ToggleButton"); JCheckBox cb = new JCheckBox("CheckBox"); JRadioButton radio = new JRadioButton("RadioButton"); SimpleListener sl = new SimpleListener(); tog.addActionListener(sl);/*from ww w. ja v a2 s. co m*/ cb.addActionListener(sl); radio.addActionListener(sl); Box buttonBox = new Box(BoxLayout.Y_AXIS); buttonBox.add(tog); buttonBox.add(cb); buttonBox.add(radio); undoButton.setEnabled(false); redoButton.setEnabled(false); undoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { try { edit.undo(); } catch (CannotUndoException ex) { ex.printStackTrace(); } finally { undoButton.setEnabled(edit.canUndo()); redoButton.setEnabled(edit.canRedo()); } } }); redoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { try { edit.redo(); } catch (CannotRedoException ex) { ex.printStackTrace(); } finally { undoButton.setEnabled(edit.canUndo()); redoButton.setEnabled(edit.canRedo()); } } }); Box undoRedoBox = new Box(BoxLayout.X_AXIS); undoRedoBox.add(Box.createGlue()); undoRedoBox.add(undoButton); undoRedoBox.add(Box.createHorizontalStrut(2)); undoRedoBox.add(redoButton); undoRedoBox.add(Box.createGlue()); Container content = getContentPane(); content.setLayout(new BorderLayout()); content.add(buttonBox, BorderLayout.CENTER); content.add(undoRedoBox, BorderLayout.SOUTH); setSize(400, 150); }