package com.mindprod.example;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
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;
import static java.lang.System.*;
/**
* demonstrate use of javax.swing.ButtonGroup with javax.swing.JToggleButton.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.1 2011-09-10 notes about Nimbus and opaque
* @since 2009-01-01
*/
public final class TestJToggleButton
{
private static final Color FOREGROUND_FOR_LABEL = 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()
{
try
{
UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
}
catch ( Exception e )
{
err.println( "Can't install Nimbus L&F" );
}
final JFrame jFrame = new JFrame();
final Container contentPane = jFrame.getContentPane();
final ButtonGroup flowers = new ButtonGroup();
final JToggleButton daffodil =
new JToggleButton( "daffodil", true );
daffodil.setForeground( FOREGROUND_FOR_LABEL );
daffodil.setBackground( Color.YELLOW );
out.println( daffodil.isOpaque() );
daffodil.setOpaque( true );
daffodil.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
final JToggleButton impatiens =
new JToggleButton( "impatiens", false );
final JToggleButton sunflower =
new JToggleButton( "sunflower", false );
final ItemListener flowerListener = new ItemListener()
{
public void itemStateChanged( ItemEvent e )
{
out.println( "daffodil:" + daffodil.isSelected() );
out.println( "impatiens:"
+ impatiens.isSelected() );
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( 300, 100 );
jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jFrame.validate();
jFrame.setVisible( true );
}
} );
}
}