// SwingGUIs3.java // One of each Swing GUI in JApplet with FlowLayout // anonymous Event handler class per GUI import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class SwingGUIs3 extends JApplet { private JLabel myLabel; private JTextField myTextField; private JPasswordField myPasswordField; private JButton myButton; private JTextArea myTextArea; private JCheckBox myCheckBox1, myCheckBox2; private JRadioButton myRadioButton1, myRadioButton2; private ButtonGroup myButtonGroup; private JComboBox myComboBox; private JList myList; public void init() { // get applet's content pane and change its layout to FlowLayout Container container = getContentPane(); container.setLayout( new FlowLayout() ); myLabel = new JLabel( "My JLabel" ); container.add( myLabel ); myTextField = new JTextField( "My JTextField", 20 ); container.add( myTextField ); //myTextField.setEditable( false ); myTextField.addActionListener( //instantiate an ActionListener object new ActionListener() { public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog(null,"myTextField got it: " + event.getActionCommand() ); //myTextField.setText( "" ); ((JTextField)(event.getSource())).setText( "" ); } } ); myPasswordField = new JPasswordField( "", 20 ); container.add( myPasswordField ); myPasswordField.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog(null,"myPasswordField got it: " + event.getActionCommand()+ " oops" ); //myPasswordField.setText( "" ); ((JPasswordField)(event.getSource())).setText( "" ); } } ); myButton = new JButton( "My JButton" ); container.add( myButton ); myButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog(null,"myButton got it"); } } ); myCheckBox1 = new JCheckBox( "My JCheckBox 1" ); container.add( myCheckBox1 ); myCheckBox1.addItemListener( new ItemListener() { public void itemStateChanged( ItemEvent event ) { if (event.getStateChange() == ItemEvent.SELECTED) JOptionPane.showMessageDialog(null,"myCheckBox 1 selected"); else if (event.getStateChange() == ItemEvent.DESELECTED) JOptionPane.showMessageDialog(null,"myCheckBox 1 deselected"); } } ); myCheckBox2 = new JCheckBox( "My JCheckBox 2" ); container.add( myCheckBox2 ); myCheckBox2.addItemListener( new ItemListener() { public void itemStateChanged( ItemEvent event ) { if ( myCheckBox2.isSelected() ) //alternate way JOptionPane.showMessageDialog(null,"myCheckBox 2 selected"); else if (event.getStateChange() == ItemEvent.DESELECTED) JOptionPane.showMessageDialog(null,"myCheckBox 2 deselected"); } } ); myRadioButton1 = new JRadioButton( "My JRadioButton 1" ); container.add( myRadioButton1 ); myRadioButton1.addItemListener( new ItemListener() { public void itemStateChanged( ItemEvent event ) { if (event.getStateChange() == ItemEvent.SELECTED) JOptionPane.showMessageDialog(null,"myRadioButton 1 selected"); else if (event.getStateChange() == ItemEvent.DESELECTED) JOptionPane.showMessageDialog(null,"myRadioButton 1 deselected"); } } ); myRadioButton2 = new JRadioButton( "My JRadioButton 2" ); container.add( myRadioButton2 ); myRadioButton2.addItemListener( new ItemListener() { public void itemStateChanged( ItemEvent event ) { if ( myRadioButton2.isSelected() ) JOptionPane.showMessageDialog(null,"myRadioButton 2 selected"); else if (event.getStateChange() == ItemEvent.DESELECTED) JOptionPane.showMessageDialog(null,"myRadioButton 2 deselected"); } } ); myButtonGroup = new ButtonGroup(); myButtonGroup.add(myRadioButton1); myButtonGroup.add(myRadioButton2); String [] names = {"fred", "jane", "steve", "susan", "bob"}; myComboBox = new JComboBox(names); myComboBox.setMaximumRowCount(3); container.add( myComboBox ); myComboBox.addItemListener( new ItemListener() { public void itemStateChanged( ItemEvent event ) { if ( event.getStateChange() == ItemEvent.SELECTED ) JOptionPane.showMessageDialog(null,"myComboBox selected index:" + myComboBox.getSelectedIndex()); else if ( event.getStateChange() == ItemEvent.DESELECTED ) JOptionPane.showMessageDialog(null,"myComboBox deselected index:" + myComboBox.getSelectedIndex()); } } ); myList = new JList(names); myList.setVisibleRowCount(3); myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); container.add(new JScrollPane(myList)); myList.addListSelectionListener( new ListSelectionListener() { public void valueChanged( ListSelectionEvent event ) { JOptionPane.showMessageDialog(null,"myList index: " + myList.getSelectedIndex()); } } ); } public void paint (Graphics g) { super.paint(g); //draw random line to see if alive g.drawLine((int)(Math.random()*getWidth()), (int)(Math.random()*getHeight()), (int)(Math.random()*getWidth()), (int)(Math.random()*getHeight())); } }