React Radio button event : Radio Button « Swing JFC « Java






React Radio button event

React Radio button event
  

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class RadioButtonTest extends JFrame implements ActionListener {
  JLabel label = new JLabel("The quick brown fox jumps over the lazy dog.");

  private JRadioButton smallButton;

  private JRadioButton mediumButton;

  private JRadioButton largeButton;

  private JRadioButton xlargeButton;

  public RadioButtonTest() {
    setTitle("RadioButtonTest");
    setSize(400, 200);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    JPanel buttonPanel = new JPanel();
    ButtonGroup group = new ButtonGroup();
    smallButton = addRadioButton(buttonPanel, group, "Small", false);
    mediumButton = addRadioButton(buttonPanel, group, "Medium", true);
    largeButton = addRadioButton(buttonPanel, group, "Large", false);
    xlargeButton = addRadioButton(buttonPanel, group, "Extra large", false);
    getContentPane().add(buttonPanel, "South");

    getContentPane().add(label, "Center");
  }

  public JRadioButton addRadioButton(JPanel buttonPanel, ButtonGroup g,
      String buttonName, boolean v) {
    JRadioButton button = new JRadioButton(buttonName, v);
    button.addActionListener(this);
    g.add(button);
    buttonPanel.add(button);
    return button;
  }

  public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == smallButton)
      label.setFont(new Font("SansSerif", Font.PLAIN, 8));
    else if (source == mediumButton)
      label.setFont(new Font("SansSerif", Font.PLAIN, 12));
    else if (source == largeButton)
      label.setFont(new Font("SansSerif", Font.PLAIN, 14));
    else if (source == xlargeButton)
      label.setFont(new Font("SansSerif", Font.PLAIN, 18));
  }

  public static void main(String[] args) {
    JFrame frame = new RadioButtonTest();
    frame.show();
  }
}
           
         
    
  








Related examples in the same category

1.Creating a JRadioButton Component
2.Radio Button Mnemonic KeyRadio Button Mnemonic Key
3.Using JRadioButtonsUsing JRadioButtons
4.A ButtonGroup voting boothA ButtonGroup voting booth
5.RadioButton DemoRadioButton Demo
6.Working with the JRadioButtonWorking with the JRadioButton
7.ButtonGroup DemoButtonGroup Demo
8.Group RadioButtonGroup RadioButton
9.Group Action RadioButtonGroup Action RadioButton
10.Implement group of buttons in an application
11.Selecting a JRadioButton Component in a Button Group
12.A frame with a sample text label and radio buttons for selecting font sizesA frame with a sample text label and radio buttons for selecting font sizes