package com.mindprod.example;
import com.mindprod.common11.FontFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
* demonstrate the use of javax.swing.ButtonGroup with javax.swing.JRadioButton
* <p/>
* composed with IntelliJ IDEA
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0
*/
public final class TestJRadioButton
{
private static final Color LABEL_FOREGROUND = new Color( 0x0000b0 );
/**
* Debugging harness for a Frame
*
* @param args command line arguments are ignored.
*/
public static void main( String args[] )
{
SwingUtilities.invokeLater( new Runnable()
{
/**
* fire up a JFrame on the Swing thread
*/
public void run()
{
final JFrame jFrame = new JFrame();
final Container contentPane = jFrame.getContentPane();
final ButtonGroup flowers = new ButtonGroup();
final JRadioButton daffodil =
new JRadioButton( "daffodil", true );
daffodil.setForeground( LABEL_FOREGROUND );
daffodil.setBackground( Color.YELLOW );
daffodil.setFont( FontFactory.build( "Dialog", Font.BOLD, 15 ) );
final JRadioButton impatiens =
new JRadioButton( "impatiens", false );
final JRadioButton sunflower =
new JRadioButton( "sunflower", false );
final ItemListener flowerListener = new ItemListener()
{
public void itemStateChanged( ItemEvent e )
{
System.out
.println( "daffodil:" + daffodil.isSelected() );
System.out
.println( "impatiens:"
+ impatiens.isSelected() );
System.out
.println( "sunflower:"
+ sunflower.isSelected() );
}
};
daffodil.addItemListener( flowerListener );
impatiens.addItemListener( flowerListener );
sunflower.addItemListener( flowerListener );
flowers.add( daffodil );
flowers.add( impatiens );
flowers.add( sunflower );
contentPane.add( daffodil, BorderLayout.WEST );
contentPane.add( impatiens, BorderLayout.CENTER );
contentPane.add( sunflower, BorderLayout.EAST );
jFrame.setSize( 400, 100 );
jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jFrame.validate();
jFrame.setVisible( true );
}
} );
}
}