Example
Home ] Up ] [ Example ] Button Group Usage ]

 

 

Here's an example:

package swingExamples;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class RadioButtonsPanel extends JPanel
{
  public RadioButtonsPanel()
  {
    setLayout(new BorderLayout());
    add(new TextPanel(), BorderLayout.CENTER);
    add(new InputPanel(), BorderLayout.SOUTH);
  }
  
  void setTextSize()
  {
    m_text.setFont(
        new Font("SansSerif", Font.BOLD, m_size) );
  }
  
  /////// Private data /////
  
  // Font sizes
  private static final int TINY = 6;
  private static final int MEDIUM = 12;
  private static final int LARGE = 14;
  private static final int HUMONGOUS = 20;
  
  private int m_size = MEDIUM; 
                      // Current font size
  
  private JLabel m_text = 
      new JLabel(
      "The quick brown fox jumps over the lazy dog");
  
  /////// Inner classes /////
  class TextPanel extends JPanel
  {
    public TextPanel()
    {
      setBackground(Color.WHITE);
      setTextSize();
      add(m_text);
    }
  }
  
  class InputPanel extends JPanel 
      implements ActionListener
  {
    public InputPanel()
    {
      addRadioButton(m_tiny);
      addRadioButton(m_medium);
      addRadioButton(m_large);
      addRadioButton(m_humongous);
    }
    
    public void actionPerformed(ActionEvent ev)
    {
      Object source = ev.getSource();
      if (source == m_tiny)
      {
        m_size = TINY;
      }
      else if (source == m_medium)
      {
        m_size = MEDIUM;
      }
      else if (source == m_large)
      {
        m_size = LARGE;
      }
      else if (source == m_humongous)
      {
        m_size = HUMONGOUS;
      }
      setTextSize();
    }
    
    private JRadioButton addRadioButton(JRadioButton button)
    {
      add(button);
      m_group.add(button);
      button.addActionListener(this);
      return button;
    }
    
    ///// Private data /////
    private ButtonGroup m_group = 
			  new ButtonGroup();
    private JRadioButton m_tiny = 
			  new JRadioButton("Tiny");
    private JRadioButton m_medium = 
			  new JRadioButton("Medium", true);
    private JRadioButton m_large = 
			  new JRadioButton("Large");
    private JRadioButton m_humongous = 
			  new JRadioButton("Humongous");
  }
}

class RadioButtonsFrame extends JFrame
{
  public RadioButtonsFrame()
  {
    setTitle("RadioButtons");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.add( new RadioButtonsPanel() );
  }
}

public class RadioButtons
{
  public static void main(String[] args)
  {
    RadioButtonsFrame frame = 
                    new RadioButtonsFrame();
    frame.setVisible(true);
  }
}

which produces:

 

This page was last modified on 02 October, 2007