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






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 = (AbstractButton) ctor.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(), 500, 300);
  }

  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.Creating a JButton Component from Action
2.Various Swing buttonsVarious Swing buttons
3.Responding to button pressesResponding to button presses
4.Putting buttons on an appletPutting buttons on an applet
5.The Swing-ified button exampleThe Swing-ified button example
6.Create a JButton that does not show focusCreate a JButton that does not show focus
7.Demonstration of button events including Action, Item and Change event typesDemonstration of button events including Action, Item and Change event types
8.The event demonstration program for JToggleButtonThe event demonstration program for JToggleButton
9.An example of radio button menu items in actionAn example of radio button menu items in action
10.JToggleButton DemoJToggleButton Demo
11.A Selected ButtonA Selected Button
12.Button demo: Mnemonic, alignment and action command Button demo: Mnemonic, alignment and action command
13.Button action to change the panel backgroundButton action to change the panel background
14.HTML buttonHTML button
15.Creating a Multiline Label for a JButton Component
16.Lines are left justified. This label text will center the lines
17.Label text italicizes the second line
18.Changing the Label of a JButton Component
19.Ploygon ButtonPloygon Button
20.Simple Button UISimple Button UI
21.Displaying a Button with an Icon LabelDisplaying a Button with an Icon Label
22.Displaying a Button with a BorderDisplaying a Button with a Border
23.Displaying a Button with Varying IconsDisplaying a Button with Varying Icons
24.Displaying a Button with Various Label AlignmentsDisplaying a Button with Various Label Alignments
25.Working with Tooltip TextWorking with Tooltip Text
26.Sharing Models between Buttons
27.Sharing an Action between JButton ComponentsSharing an Action between JButton Components
28.Display Message
29.Button Html DemoButton Html Demo
30.Swing Button DemoSwing Button Demo
31.LoadSave: Button actionLoadSave: Button action
32.Button with ICON and Multiline Button with ICON and Multiline
33.Action Button SampleAction Button Sample
34.Mnemonic SampleMnemonic Sample
35.Swing Default ButtonSwing Default Button
36.Button Action SampleButton Action Sample
37.Button icons, a default button, HTML in a button,and button mnemonics.
38.Adding a Rollover and Pressed Icon to a JButton Component
39.Adding a Disabled Icon to a JButton Component
40.Setting the Gap Size Between the Label and Icon in a JButton Component
41.Moving the Icon in a JButton Component
42.Adding an Icon to a JButton Component
43.Moving the Label/Icon Pair in a JButton Component
44.If the action does not have an icon or a different icon must be used, add or change the icon using setIcon():
45.Dynamically update the appearance of a component
46.Simple ToggleButton SampleSimple ToggleButton Sample