/* * [TestEnumJComboBox.java] * * Summary: demonstrate how to use a GUI to input enums with a javax.swing.JComboBox. * * Copyright: (c) 2010-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 2010-04-03 initial version */ package com.mindprod.example; 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; import static java.lang.System.*; /** * demonstrate how to use a GUI to input enums with a javax.swing.JComboBox. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2010-04-03 initial version * @since 2010-04-03 */ public final class TestEnumJComboBox { 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 jFrame = new JFrame(); final Container contentPane = jFrame.getContentPane(); contentPane.setLayout( new FlowLayout() ); final JComboBox variety = new JComboBox<>( GrapefruitVariety.values() ); // Ensure all possible choices will be displayed without scrolling. // At rest only one will be displayed. variety.setMaximumRowCount( GrapefruitVariety.values().length ); variety.setForeground( FOREGROUND_FOR_LABEL ); variety.setBackground( Color.WHITE ); variety.setFont( new Font( "Dialog", Font.BOLD, 15 ) ); // turn off the write-in feature variety.setEditable( false ); // setting the selection variety.setSelectedIndex( 0 ); // alternatively, by value. variety.setSelectedItem( GrapefruitVariety.GOLDEN ); // no multiple selections permitted. variety.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 ) { // We don't need to cast because even Object supports toString which we have overridden. String choice = variety.getSelectedItem().toString(); out.println( choice ); } } ); contentPane.add( variety ); jFrame.setSize( 100, 100 ); jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); jFrame.validate(); jFrame.setVisible( true ); } } ); } // end main /* enum for varieties of grapefruit */ enum GrapefruitVariety { GOLDEN( "Golden" ), MARSH( "Marsh Ruby" ), RIO( "Rio Red" ), RUBY( "Ruby" ), STAR( "Star Ruby" ), WHITE( "White" ); /** * human name of variety */ private final String name; /** * constructor * * @param name human name of variety */ GrapefruitVariety( String name ) { this.name = name; } /** * override standard toString * * @return name of variety */ public String toString() { return name; } } }