Uses reflection to create groups of different types of AbstractButton : Button « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » ButtonScreenshots 
Uses reflection to create groups of different types of AbstractButton
Uses reflection to create groups of different types of AbstractButton

// : c14:ButtonGroups.java
// Uses reflection to create groups
// of different types of AbstractButton.
// <applet code=ButtonGroups width=500 height=300></applet>
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.awt.Container;
import java.awt.FlowLayout;
import java.lang.reflect.Constructor;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
import javax.swing.border.TitledBorder;

public class ButtonGroups extends JApplet {
  private static String[] ids = "June""Ward""Beaver""Wally""Eddie",
      "Lumpy"};

  static JPanel makeBPanel(Class klass, String[] ids) {
    ButtonGroup bg = new ButtonGroup();
    JPanel jp = new JPanel();
    String title = klass.getName();
    title = title.substring(title.lastIndexOf('.'1);
    jp.setBorder(new TitledBorder(title));
    for (int i = 0; i < ids.length; i++) {
      AbstractButton ab = new JButton("failed");
      try {
        // Get the dynamic constructor method
        // that takes a String argument:
        Constructor ctor = klass
            .getConstructor(new Class[] { String.class });
        // Create a new object:
        ab = (AbstractButtonctor.newInstance(new Object[] { ids[i] });
      catch (Exception ex) {
        System.err.println("can't create " + klass);
      }
      bg.add(ab);
      jp.add(ab);
    }
    return jp;
  }

  public void init() {
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(makeBPanel(JButton.class, ids));
    cp.add(makeBPanel(JToggleButton.class, ids));
    cp.add(makeBPanel(JCheckBox.class, ids));
    cp.add(makeBPanel(JRadioButton.class, ids));
  }

  public static void main(String[] args) {
    run(new ButtonGroups()500300);
  }

  public static void run(JApplet applet, int width, int height) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(applet);
    frame.setSize(width, height);
    applet.init();
    applet.start();
    frame.setVisible(true);
  }
///:~



           
       
Related examples in the same category
1. Various Swing buttonsVarious Swing buttons
2. Responding to button pressesResponding to button presses
3. Putting buttons on an appletPutting buttons on an applet
4. The Swing-ified button exampleThe Swing-ified button example
5. Create a JButton that does not show focusCreate a JButton that does not show focus
6. Demonstration of button events including Action, Item and Change event typesDemonstration of button events including Action, Item and Change event types
7. The event demonstration program for JToggleButtonThe event demonstration program for JToggleButton
8. An example of radio button menu items in actionAn example of radio button menu items in action
9. JToggleButton DemoJToggleButton Demo
10. A Selected ButtonA Selected Button
11. Button demo: Mnemonic, alignment and action command Button demo: Mnemonic, alignment and action command
12. Button action to change the panel backgroundButton action to change the panel background
13. HTML buttonHTML button
14. Ploygon ButtonPloygon Button
15. Simple Buttin UISimple Buttin UI
16. Displaying a Button with an Icon LabelDisplaying a Button with an Icon Label
17. Displaying a Button with a BorderDisplaying a Button with a Border
18. Displaying a Button with Varying IconsDisplaying a Button with Varying Icons
19. Displaying a Button with Various Label AlignmentsDisplaying a Button with Various Label Alignments
20. Working with Tooltip TextWorking with Tooltip Text
21. Sharing Models between Buttons
22. Sharing an Action between JButton ComponentsSharing an Action between JButton Components
23. Display Message
24. Button Html DemoButton Html Demo
25. Swing Button DemoSwing Button Demo
26. LoadSave: Button actionLoadSave: Button action
27. Button with ICON and Multiline Button with ICON and Multiline
28. Action Button SampleAction Button Sample
29. Mnemonic SampleMnemonic Sample
30. Swing Default ButtonSwing Default Button
31. Button Action SampleButton Action Sample
32. Simple ToggleButton SampleSimple ToggleButton Sample
w__ww.___j__a_v__a___2_s_.___c__o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.