List of usage examples for javax.swing JRadioButton addActionListener
public void addActionListener(ActionListener l)
ActionListener
to the button. From source file:JRadioButtonActionListener.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 sliceActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton aButton = (AbstractButton) actionEvent.getSource(); System.out.println("Selected: " + aButton.getText()); }// w w w . j a va 2s. com }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addActionListener(sliceActionListener); bRadioButton.addActionListener(sliceActionListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.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 ww w . ja v a 2 s . co 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); }
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 w w w. j a v a 2s .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:Main.java
public static void main(String[] args) { JRadioButton dem = new JRadioButton("Bill", false); dem.setActionCommand("Bill"); JRadioButton rep = new JRadioButton("Bob", false); rep.setActionCommand("Bob"); JRadioButton ind = new JRadioButton("Ross", false); ind.setActionCommand("Ross"); final ButtonGroup group = new ButtonGroup(); group.add(dem);/*w w w. j a va 2s . c om*/ group.add(rep); group.add(ind); class VoteActionListener implements ActionListener { public void actionPerformed(ActionEvent ex) { String choice = group.getSelection().getActionCommand(); System.out.println("ACTION Candidate Selected: " + choice); } } class VoteItemListener implements ItemListener { public void itemStateChanged(ItemEvent ex) { String item = ((AbstractButton) ex.getItemSelectable()).getActionCommand(); boolean selected = (ex.getStateChange() == ItemEvent.SELECTED); System.out.println("ITEM Candidate Selected: " + selected + " Selection: " + item); } } ActionListener al = new VoteActionListener(); dem.addActionListener(al); rep.addActionListener(al); ind.addActionListener(al); ItemListener il = new VoteItemListener(); dem.addItemListener(il); rep.addItemListener(il); ind.addItemListener(il); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = frame.getContentPane(); c.setLayout(new GridLayout(4, 1)); c.add(new JLabel("Please Cast Your Vote")); c.add(dem); c.add(rep); c.add(ind); frame.pack(); frame.setVisible(true); }
From source file:SimpleButtonGroupExample.java
public static void main(String[] args) { // Some choices JRadioButton choice1, choice2, choice3; choice1 = new JRadioButton("Bach: Well Tempered Clavier, Book I"); choice1.setActionCommand("bach1"); choice2 = new JRadioButton("Bach: Well Tempered Clavier, Book II"); choice2.setActionCommand("bach2"); choice3 = new JRadioButton("Shostakovich: 24 Preludes and Fugues"); choice3.setActionCommand("shostakovich"); // A group, to ensure that we only vote for one. final ButtonGroup group = new ButtonGroup(); group.add(choice1);//www .ja v a 2 s. c o m group.add(choice2); group.add(choice3); // A simple ActionListener, showing each selection using the ButtonModel class VoteActionListener implements ActionListener { public void actionPerformed(ActionEvent ev) { String choice = group.getSelection().getActionCommand(); System.out.println("ACTION Choice Selected: " + choice); } } // A simple ItemListener, showing each selection and deselection class VoteItemListener implements ItemListener { public void itemStateChanged(ItemEvent ev) { boolean selected = (ev.getStateChange() == ItemEvent.SELECTED); AbstractButton button = (AbstractButton) ev.getItemSelectable(); System.out .println("ITEM Choice Selected: " + selected + ", Selection: " + button.getActionCommand()); } } // Add listeners to each button ActionListener alisten = new VoteActionListener(); choice1.addActionListener(alisten); choice2.addActionListener(alisten); choice3.addActionListener(alisten); ItemListener ilisten = new VoteItemListener(); choice1.addItemListener(ilisten); choice2.addItemListener(ilisten); choice3.addItemListener(ilisten); // Throw everything together JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = frame.getContentPane(); c.setLayout(new GridLayout(0, 1)); c.add(new JLabel("Vote for your favorite prelude & fugue cycle")); c.add(choice1); c.add(choice2); c.add(choice3); frame.pack(); frame.setVisible(true); }
From source file:Main.java
/** * Creates a radio button with all the properties set up front. * // w w w . ja va 2 s .c o m * @param name * The caption of the button. * @param command * The name of the command the listener should look for. * @param isSelected * The condition that should be true if this control is selected. * @param listener * The object listening for commands. * @return A configured radio button. */ public static JRadioButton createRadioButton(String name, String command, boolean isSelected, ActionListener listener) { JRadioButton button; button = new JRadioButton(name); button.setActionCommand(command); button.setSelected(isSelected); button.addActionListener(listener); return button; }
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 .j a va 2 s.co m*/ } for (ActionListener listener : listeners) { result.addActionListener(listener); } return result; }
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);//from ww w.j a 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; }
From source file:BorderTest.java
public void addRadioButton(String buttonName, final Border b) { JRadioButton button = new JRadioButton(buttonName); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { demoPanel.setBorder(b);/* w w w.j a v a 2s . c o m*/ } }); group.add(button); buttonPanel.add(button); }
From source file:MainClass.java
public MainClass() { theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cp = theFrame.getContentPane();// w ww . ja va2 s . c o m cp.setLayout(new FlowLayout()); ButtonGroup bg = new ButtonGroup(); JRadioButton bJava = new JRadioButton("Java"); bJava.addActionListener(new LNFSetter("javax.swing.plaf.metal.MetalLookAndFeel", bJava)); bg.add(bJava); cp.add(bJava); JRadioButton bMSW = new JRadioButton("MS-Windows"); bMSW.addActionListener(new LNFSetter("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", bMSW)); bg.add(bMSW); cp.add(bMSW); JRadioButton bMotif = new JRadioButton("Motif"); bMotif.addActionListener(new LNFSetter("com.sun.java.swing.plaf.motif.MotifLookAndFeel", bMotif)); bg.add(bMotif); cp.add(bMotif); JRadioButton bMac = new JRadioButton("Sun-MacOS"); bMac.addActionListener(new LNFSetter("com.sun.java.swing.plaf.mac.MacLookAndFeel", bMac)); bg.add(bMac); cp.add(bMac); String defaultLookAndFeel = UIManager.getSystemLookAndFeelClassName(); JRadioButton bDefault = new JRadioButton("Default"); bDefault.addActionListener(new LNFSetter(defaultLookAndFeel, bDefault)); bg.add(bDefault); cp.add(bDefault); (previousButton = bDefault).setSelected(true); theFrame.pack(); theFrame.setVisible(true); }