Results 1 to 1 of 1
Thread: Reacting Radio button event
-
Reacting Radio button event
Java Code: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(); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Disable Radio button
By AJG in forum New To JavaReplies: 3Last Post: 05-10-2011, 11:09 AM -
Swing - catching click button event
By Java Tip in forum Java TipReplies: 0Last Post: 03-11-2008, 11:03 PM -
AWT - catching click button event
By Java Tip in forum Java TipReplies: 0Last Post: 03-11-2008, 11:02 PM -
How to perform some event to button click
By eva in forum AWT / SwingReplies: 2Last Post: 01-16-2008, 12:27 AM -
Radio button values are not lose
By chockalingam in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-03-2007, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks