Group Action RadioButton : Radio Button « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » Radio ButtonScreenshots 
Group Action RadioButton
Group Action RadioButton
 
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class GroupActionRadio {
  private static final String sliceOptions[] "4 slices""8 slices",
      "12 slices""16 slices" };

  private static final String crustOptions[] "Sicilian""Thin Crust",
      "Thick Crust""Stuffed Crust" };

  public static void main(String args[]) {

    String title = (args.length == "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 = (AbstractButtonactionEvent
            .getSource();
        System.out.println("Selected: " + aButton.getText());
      }
    };
    Container sliceContainer = RadioButtonUtils.createRadioButtonGrouping(
        sliceOptions, "Slice Count", sliceActionListener);

    // Crust Parts
    ActionListener crustActionListener = new ActionListener() {
      String lastSelected;

      public void actionPerformed(ActionEvent actionEvent) {
        AbstractButton aButton = (AbstractButtonactionEvent
            .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 = (AbstractButtonitemEvent.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 = (AbstractButtonchangEvent
            .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(300200);
    frame.setVisible(true);
  }
}

class RadioButtonUtils {
  private RadioButtonUtils() {
    // private constructor so you can't create instances
  }

  public static Enumeration getSelectedElements(Container container) {
    Vector selections = new Vector();
    Component components[] = container.getComponents();
    for (int i = 0, n = components.length; i < n; i++) {
      if (components[iinstanceof AbstractButton) {
        AbstractButton button = (AbstractButtoncomponents[i];
        if (button.isSelected()) {
          selections.addElement(button.getText());
        }
      }
    }
    return selections.elements();
  }

  public static Container createRadioButtonGrouping(String elements[]) {
    return createRadioButtonGrouping(elements, null, null, null, null);
  }

  public static Container createRadioButtonGrouping(String elements[],
      String title) {
    return createRadioButtonGrouping(elements, title, null, null, null);
  }

  public static Container createRadioButtonGrouping(String elements[],
      String title, ItemListener itemListener) {
    return createRadioButtonGrouping(elements, title, null, itemListener,
        null);
  }

  public static Container createRadioButtonGrouping(String elements[],
      String title, ActionListener actionListener) {
    return createRadioButtonGrouping(elements, title, actionListener, null,
        null);
  }

  public static Container createRadioButtonGrouping(String elements[],
      String title, ActionListener actionListener,
      ItemListener itemListener) {
    return createRadioButtonGrouping(elements, title, actionListener,
        itemListener, null);
  }

  public static Container createRadioButtonGrouping(String elements[],
      String title, ActionListener actionListener,
      ItemListener itemListener, ChangeListener changeListener) {
    JPanel panel = new JPanel(new GridLayout(01));
    //   If title set, create titled border
    if (title != null) {
      Border border = BorderFactory.createTitledBorder(title);
      panel.setBorder(border);
    }
    //   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;
  }
}

           
         
  
Related examples in the same category
1. Creating a JRadioButton Component
2. Radio Button Mnemonic KeyRadio Button Mnemonic Key
3. Using JRadioButtonsUsing JRadioButtons
4. A ButtonGroup voting boothA ButtonGroup voting booth
5. RadioButton DemoRadioButton Demo
6. React Radio button eventReact Radio button event
7. Working with the JRadioButtonWorking with the JRadioButton
8. ButtonGroup DemoButtonGroup Demo
9. Group RadioButtonGroup RadioButton
10. Implement group of buttons in an application
11. Selecting a JRadioButton Component in a Button Group
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.