package com.mindprod.example;
import com.mindprod.common11.FontFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
* demonstrate the use of javax.swing.JList
* <p/>
* composed with IntelliJ IDEA
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0
*/
public final class TestJComboBox
{
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()
{
public void run()
{
final JFrame jFrame = new JFrame();
final Container contentPane = jFrame.getContentPane();
contentPane.setLayout( new FlowLayout() );
final JComboBox flavour = new JComboBox( new String[] {
"strawberry", "chocolate", "vanilla" } );
flavour.setForeground( LABEL_FOREGROUND );
flavour.setBackground( Color.WHITE );
flavour.setFont( FontFactory.build( "Dialog", Font.BOLD, 15 ) );
flavour.setEditable( false );
flavour.setSelectedIndex( 0 );
flavour.setSelectedItem( "chocolate" );
flavour.addItemListener( new ItemListener()
{
/**
* Called whenever the value of the selection changes. Will
* be called twice for every change.
*
* @param e the event that characterizes the change.
*/
public void itemStateChanged( ItemEvent e )
{
String choice = ( String ) flavour.getSelectedItem();
System.out.println( choice );
}
} );
contentPane.add( flavour );
jFrame.setSize( 100, 100 );
jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jFrame.validate();
jFrame.setVisible( true );
}
} );
}
}