import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
public class Combobox extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JPanel pane = new JPanel();
JButton pressme = new JButton("Press Me");
JLabel answer = new JLabel("");
String[] animals = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox animalscombobox = new JComboBox( animals );
Combobox()
{
super( "Button Press" );
setBounds( 100 , 100 , 400 , 300 );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container myContainer = this.getContentPane(); // inherit main frame
myContainer.add( pane );
pressme.setMnemonic( 'P' );
pane.add( pressme );
pressme.addActionListener(this);
pressme.requestFocus();
answer.setText( "Please press the button" );
pane.add( answer );
pane.add( animalscombobox );
animalscombobox.setSelectedIndex( 4 );
animalscombobox.addActionListener( this );
setVisible(true);
}
public void actionPerformed( ActionEvent event )
{
Object source = event.getSource();
if( source == pressme )
{
answer.setText( "Button pressed!" );
JOptionPane.showMessageDialog( null , "Popup from button." , "Message Dialog" , JOptionPane.PLAIN_MESSAGE );
setVisible(true);
}
if( source == animalscombobox )
{
answer.setText( "animals!" );
JComboBox cb = (JComboBox) event.getSource();
String name = (String) cb.getSelectedItem();
setVisible(true);
}
}
public static void main( String args[] )
{ new Combobox();
}
}