Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Thinking in Java
Prev Contents / Index Next

A catalog of Swing components

Now that you understand layout managers and the event model, you’re ready to see how Swing components can be used. This section is a non-exhaustive tour of the Swing components and features that you’ll probably use most of the time. Each example is intended to be reasonably small so that you can easily lift the code and use it in your own programs.

Keep in mind:

  1. You can easily see what each of these examples looks like while running by viewing the HTML pages in the downloadable source code for this chapter (
  2. The JDK documentation from contains all of the Swing classes and methods (only a few are shown here).
  3. Because of the naming convention used for Swing events, it’s fairly easy to guess how to write and install a handler for a particular type of event. Use the lookup program from earlier in this chapter to aid in your investigation of a particular component.
  4. When things start to get complicated you should graduate to a GUI builder. Buttons

    Swing includes a number of different types of buttons. All buttons, check boxes, radio buttons, and even menu items are inherited from AbstractButton (which, since menu items are included, would probably have been better named “AbstractSelector” or something equally general). You’ll see the use of menu items shortly, but the following example shows the various types of buttons available:

    // Various Swing buttons.
    // <applet code=Buttons width=350 height=100></applet>
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.plaf.basic.*;
    import javax.swing.border.*;
    import com.bruceeckel.swing.*;
    public class Buttons extends JApplet {
      private JButton jb = new JButton("JButton");
      private BasicArrowButton
        up = new BasicArrowButton(BasicArrowButton.NORTH),
        down = new BasicArrowButton(BasicArrowButton.SOUTH),
        right = new BasicArrowButton(BasicArrowButton.EAST),
        left = new BasicArrowButton(BasicArrowButton.WEST);
      public void init() {
        Container cp = getContentPane();
        cp.setLayout(new FlowLayout());
        cp.add(new JToggleButton("JToggleButton"));
        cp.add(new JCheckBox("JCheckBox"));
        cp.add(new JRadioButton("JRadioButton"));
        JPanel jp = new JPanel();
        jp.setBorder(new TitledBorder("Directions"));
      public static void main(String[] args) { Buttons(), 350, 100);
    } ///:~

    This begins with the BasicArrowButton from javax.swing.plaf.basic, then continues with the various specific types of buttons. When you run the example, you’ll see that the toggle button holds its last position, in or out. But the check boxes and radio buttons behave identically to each other, just clicking on or off (they are inherited from JToggleButton).

    Button groups

    If you want radio buttons to behave in an “exclusive or” fashion, you must add them to a “button group.” But, as the following example demonstrates, any AbstractButton can be added to a ButtonGroup.

    To avoid repeating a lot of code, this example uses reflection to generate the groups of different types of buttons. This is seen in makeBPanel( ), which creates a button group and a JPanel. The second argument to makeBPanel( ) is an array of String. For each String, a button of the class represented by the first argument is added to the JPanel:

    // Uses reflection to create groups
    // of different types of AbstractButton.
    // <applet code=ButtonGroups width=500 height=300></applet>
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import java.lang.reflect.*;
    import com.bruceeckel.swing.*;
    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);
        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) { ButtonGroups(), 500, 300);
    } ///:~

    The title for the border is taken from the name of the class, stripping off all the path information. The AbstractButton is initialized to a JButton that has the label “Failed,” so if you ignore the exception message, you’ll still see the problem on screen. The getConstructor( ) method produces a Constructor object that takes the array of arguments of the types in the Class array passed to getConstructor( ). Then all you do is call newInstance( ), passing it an array of Object containing your actual arguments—in this case, just the String from the ids array.

    This adds a little complexity to what is a simple process. To get “exclusive or” behavior with buttons, you create a button group and add each button for which you want that behavior to the group. When you run the program, you’ll see that all the buttons except JButton exhibit this “exclusive or” behavior.
    Thinking in Java
    Prev Contents / Index Next

   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire