package uiintro;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class RadioButtonDemo extends JFrame implements ActionListener {
  private static String JAVA = "Java";
  private static String CPP = "C++";
  private static String LISP = "LISP";
  private static String PERL = "Perl";
  private static String APL = "APL";

  JLabel label;

  public RadioButtonDemo() {
    this.setSize(150, 250);

    this.getContentPane().setLayout(new GridLayout(6, 1));

    label = new JLabel("Selected: NONE");
    this.getContentPane().add(label);

    // we do notification for these radio buttons slightly differently.
    // they all have the same listeners, but different ActionCommands. The
    // ActionCommand is a string parameter which will be incorporated into
    // the event structure when the buttons are pressed.
    //
    JRadioButton b1 = new JRadioButton(JAVA);
    b1.setActionCommand(JAVA);
    b1.addActionListener(this);
    this.getContentPane().add(b1);

    JRadioButton b2 = new JRadioButton(CPP);
    b2.setActionCommand(CPP);
    b2.addActionListener(this);
    this.getContentPane().add(b2);

    JRadioButton b3 = new JRadioButton(LISP);
    b3.setActionCommand(LISP);
    b3.addActionListener(this);
    this.getContentPane().add(b3);

    JRadioButton b4 = new JRadioButton(PERL);
    b4.setActionCommand(PERL);
    b4.addActionListener(this);
    this.getContentPane().add(b4);

    JRadioButton b5 = new JRadioButton(APL);
    b5.setActionCommand(APL);
    b5.addActionListener(this);
    this.getContentPane().add(b5);

    // There might be many radio buttons in an interface. We need to tell
    // Swing which set of buttons are a set with respect to each other... in
    // other words, which set of buttons constitute a group where only one
    // can be selected at a time. We do this with a ButtonGroup object.
    // Note that a ButtonGroup is a logical grouping, not a spatial grouping;
    // it has no visual element. We rely on normal layout mechanisms to arrange
    // the radio buttons on the interface.
    //
    ButtonGroup group = new ButtonGroup();
    group.add(b1); group.add(b2); group.add(b3); group.add(b4); group.add(b5);

    this.setVisible(true);
  }

  public void actionPerformed(ActionEvent ae) {
    label.setText("Selected: " + ae.getActionCommand());
  }
}