import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 * Ikkunaluokka.
 */
public class ListComboFrame extends JFrame
{
  private String data[] = { "yksi", "kaksi", "kolme", "neljä", "viisi", "kuusi", "seitsemän", "kahdeksan" };
  private JComboBox comboBox = new JComboBox( data );
  private JList list = new JList( data );
  private JLabel label = new JLabel();

  /**
   * Konstruktori.
   */
  public ListComboFrame()
  {
    JScrollPane scrollPane = new JScrollPane();
    JButton buttonCombobox = new JButton();
    JButton buttonList = new JButton();

    JPanel contentPane = ( JPanel ) getContentPane();
    contentPane.setLayout( new GridBagLayout() );
    setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
    setResizable( false );
    setSize( new Dimension( 400, 300 ) );
    setTitle( "ListBox & ComboBox" );

    buttonCombobox.setText( "Tutki ComboBox" );
    buttonCombobox.addActionListener( new java.awt.event.ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        buttonCombobox_actionPerformed( e );
      }
    } );

    buttonList.setText( "Tutki List" );
    buttonList.addActionListener( new java.awt.event.ActionListener()
    {
      public void actionPerformed( ActionEvent e )
      {
        buttonList_actionPerformed( e );
      }
    } );

    list.setSelectedIndex( 0 );
    list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    list.setVisibleRowCount( 5 );
    label.setBorder( BorderFactory.createEtchedBorder() );
    label.setText( "Valitse" );

    comboBox.setMaximumRowCount( 5 );

    contentPane.add( comboBox, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 20, 0 ), 0, 0 ) );
    contentPane.add( buttonCombobox,
                     new GridBagConstraints( 2, 1, GridBagConstraints.REMAINDER, GridBagConstraints.REMAINDER, 0.0, 0.0
                     , GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets( 0, 20, 0, 0 ), 0, 0 ) );
    contentPane.add( label, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0
        , GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
    contentPane.add( buttonList, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0
        , GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL, new Insets( 0, 20, 0, 0 ), 0, 0 ) );
    contentPane.add( scrollPane, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0
        , GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets( 0, 0, 20, 0 ), 80, 0 ) );
    scrollPane.getViewport().add( list, null );
  }

  /**
   * Listan tutkiminen.
   * @param e tapahtuman tarkempi kuvaus
   */
  private void buttonList_actionPerformed( ActionEvent e )
  {
    label.setText( list.getSelectedValue().toString() );
  }

  /**
   * Comboboxin tutkiminen.
   * @param e tapahtuman tarkempi kuvaus
   */
  private void buttonCombobox_actionPerformed( ActionEvent e )
  {
    label.setText( comboBox.getSelectedItem().toString() );
  }

}
