List of usage examples for javax.swing ButtonGroup add
public void add(AbstractButton b)
From source file:JRadioButtonSelectedElements.java
public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ActionListener crustActionListener = new ActionListener() { String lastSelected;//from www.ja v a 2 s . c o m 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); } }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addActionListener(crustActionListener); bRadioButton.addActionListener(crustActionListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); System.out.println(getSelectedElements(panel).hasMoreElements()); }
From source file:JRadioButtonItemListener.java
public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ItemListener itemListener = new ItemListener() { String lastSelected;/*from w ww .j a va 2s. c o m*/ 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); } }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addItemListener(itemListener); bRadioButton.addItemListener(itemListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); }
From source file:ScribbleDragAndDrop.java
/** * The main method. Creates a simple application using this class. Note the * buttons for switching between draw mode and drag mode. *///from w w w . ja v a2 s.c o m public static void main(String[] args) { // Create a frame and put a scribble pane in it JFrame frame = new JFrame("ScribbleDragAndDrop"); final ScribbleDragAndDrop scribblePane = new ScribbleDragAndDrop(); frame.getContentPane().add(scribblePane, BorderLayout.CENTER); // Create two buttons for switching modes JToolBar toolbar = new JToolBar(); ButtonGroup group = new ButtonGroup(); JToggleButton draw = new JToggleButton("Draw"); JToggleButton drag = new JToggleButton("Drag"); draw.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scribblePane.setDragMode(false); } }); drag.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scribblePane.setDragMode(true); } }); group.add(draw); group.add(drag); toolbar.add(draw); toolbar.add(drag); frame.getContentPane().add(toolbar, BorderLayout.NORTH); // Start off in drawing mode draw.setSelected(true); scribblePane.setDragMode(false); // Pop up the window frame.setSize(400, 400); frame.setVisible(true); }
From source file:Wallpaper.java
private static JPanel createPanel() { JPanel p = new JPanel(); ButtonGroup entreeGroup = new ButtonGroup(); JRadioButton radioButton;// ww w. j a va 2s . c om p.add(radioButton = new JRadioButton("Beef", true)); entreeGroup.add(radioButton); p.add(radioButton = new JRadioButton("Chicken")); entreeGroup.add(radioButton); p.add(radioButton = new JRadioButton("Vegetable")); entreeGroup.add(radioButton); p.add(new JCheckBox("Ketchup")); p.add(new JCheckBox("Mustard")); p.add(new JCheckBox("Pickles")); p.add(new JLabel("Special requests:")); p.add(new JTextField(20)); JButton orderButton = new JButton("Place Order"); p.add(orderButton); return p; }
From source file:Main.java
public static JRadioButton createRadioButton(String text, ButtonGroup buttonGroup, ActionListener... listeners) { JRadioButton result = new JRadioButton(text); if (buttonGroup != null) { buttonGroup.add(result); }//from w w w. ja v a 2 s . co m for (ActionListener listener : listeners) { result.addActionListener(listener); } return result; }
From source file:Main.java
/** * Groups buttons in the specified button group. * * @param buttonGroup/*from w w w . j a v a 2 s.c o m*/ * button group * @param buttons * buttons to group */ public static void groupButtons(final ButtonGroup buttonGroup, final AbstractButton... buttons) { for (final AbstractButton button : buttons) { buttonGroup.add(button); } }
From source file:MenuDemo1.java
public static JMenuItem radioItem(String label, ActionListener listener, String command, ButtonGroup mutExGroup) { JMenuItem item = new JRadioButtonMenuItem(label); item.addActionListener(listener);//from w ww. j ava 2 s .c o m item.setActionCommand(command); mutExGroup.add(item); return item; }
From source file:RadioButtonUtils.java
public static Container createRadioButtonGrouping(String elements[], String title) { JPanel panel = new JPanel(new GridLayout(0, 1)); if (title != null) { Border border = BorderFactory.createTitledBorder(title); panel.setBorder(border);// w w w . j a v a2 s. c om } ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton; for (int i = 0, n = elements.length; i < n; i++) { aRadioButton = new JRadioButton(elements[i]); panel.add(aRadioButton); group.add(aRadioButton); } return panel; }
From source file:Main.java
/** * Groups all buttons inside this container and all subcontainers if * requested and returns created button group. * * @param container//from w w w . j a v a 2s. com * container to process * @param recursive * whether to check all subcontainers or not * @param buttonGroup * button group */ public static void groupButtons(final Container container, final boolean recursive, final ButtonGroup buttonGroup) { for (final Component component : container.getComponents()) { if (component instanceof AbstractButton) { buttonGroup.add((AbstractButton) component); } if (recursive) { if (component instanceof Container) { groupButtons(container, true); } } } }
From source file:GroupRadio.java
public static Container createRadioButtonGrouping(String elements[], String title, ActionListener actionListener, ItemListener itemListener, ChangeListener changeListener) { JPanel panel = new JPanel(new GridLayout(0, 1)); // If title set, create titled border if (title != null) { Border border = BorderFactory.createTitledBorder(title); panel.setBorder(border);//w ww . ja v a 2s .co m } // Create group ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton; // For each String passed in: // Create button, add to panel, and add to group for (int i = 0, n = elements.length; i < n; i++) { aRadioButton = new JRadioButton(elements[i]); panel.add(aRadioButton); group.add(aRadioButton); if (actionListener != null) { aRadioButton.addActionListener(actionListener); } if (itemListener != null) { aRadioButton.addItemListener(itemListener); } if (changeListener != null) { aRadioButton.addChangeListener(changeListener); } } return panel; }