/*
 * [TestJCheckBox.java]
 *
 * Summary: example use of javax.swing.ButtonGroup with javax.swing.JCheckBox.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-01-01 initial version
 */
package com.mindprod.example;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
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;

import static java.lang.System.*;
// Using a ButtonGroup with JCheckBox.
// This code lets you select one of three types
// of flower with three toggle buttons.
// The ButtonGroup itself is not visible.
// It does not draw a line around the boxes, for example.
// It just ensures only one option is selected
// at an elapsedTime.
// It is up to you to layout the boxes in some
// logical pattern that indicates they belong together.

/**
 * example use of javax.swing.ButtonGroup with javax.swing.JCheckBox.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01 initial version
 * @since 2009-01-01
 */
public final class TestJCheckBox
    {
    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()
            {
            public void run()
                {
                final JFrame frame = new JFrame();
                final Container contentPane = frame.getContentPane();
                final ButtonGroup flowers = new ButtonGroup();
                final JCheckBox daffodil = new JCheckBox( "daffodil", true );
                daffodil.setForeground( FOREGROUND_FOR_LABEL );
                // this modifies the unselected/selected background colour
                daffodil.setBackground( Color.YELLOW );
                daffodil.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
                final JCheckBox impatiens = new JCheckBox( "impatiens", false );
                final JCheckBox sunflower = new JCheckBox( "sunflower", false );
                // will also trigger ActionEvents, but you don't need both.
                final ItemListener flowerListener = new ItemListener()
                    {
                    public void itemStateChanged( ItemEvent e )
                        {
                        // warning flowers.getSelection() returns a ButtonModel not the
                        // JCheckBox.
                        // Beware, you will get two events for each change, one to
                        // remove a selection
                        // and one to add a new one.
                        out.println( "daffodil:" + daffodil.isSelected() );
                        out.println( "impatiens:"
                                     + impatiens.isSelected() );
                        out.println( "sunflower:"
                                     + sunflower.isSelected() );
                        /* ... */
                        }
                    };
                daffodil.addItemListener( flowerListener );
                impatiens.addItemListener( flowerListener );
                sunflower.addItemListener( flowerListener );
                // Note you don't attach the Listener to the ButtonGroup.
                // add JCheckBoxes to ButtonGroup
                flowers.add( daffodil );
                flowers.add( impatiens );
                flowers.add( sunflower );
                // add JCheckBox to the JFrame
                contentPane.add( daffodil, BorderLayout.WEST );
                contentPane.add( impatiens, BorderLayout.CENTER );
                contentPane.add( sunflower, BorderLayout.EAST );
                frame.setSize( 300, 100 );
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.validate();
                frame.setVisible( true );
                }
            } );
        } // end main
    } // end class