// SwingGUIs2.java // One of each Swing GUI in JApplet with FlowLayout // nested Event handler class per GUI import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class SwingGUIs2 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 ); MyTextFieldHandler myTextFieldHandler = new MyTextFieldHandler(); myTextField.addActionListener( myTextFieldHandler ); myPasswordField = new JPasswordField( "", 20 ); container.add( myPasswordField ); MyPasswordFieldHandler myPasswordFieldHandler = new MyPasswordFieldHandler(); myPasswordField.addActionListener( myPasswordFieldHandler ); myButton = new JButton( "My JButton" ); container.add( myButton ); MyButtonHandler myButtonHandler = new MyButtonHandler(); myButton.addActionListener( myButtonHandler ); myCheckBox1 = new JCheckBox( "My JCheckBox 1" ); container.add( myCheckBox1 ); MyCheckBoxHandler myCheckBoxHandler = new MyCheckBoxHandler(); myCheckBox1.addItemListener( myCheckBoxHandler ); myCheckBox2 = new JCheckBox( "My JCheckBox 2" ); container.add( myCheckBox2 ); myCheckBox2.addItemListener( myCheckBoxHandler ); myRadioButton1 = new JRadioButton( "My JRadioButton 1" ); container.add( myRadioButton1 ); MyRadioButtonHandler myRadioButtonHandler = new MyRadioButtonHandler(); myRadioButton1.addItemListener( myRadioButtonHandler ); myRadioButton2 = new JRadioButton( "My JRadioButton 2" ); container.add( myRadioButton2 ); myRadioButton2.addItemListener( myRadioButtonHandler ); 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 ); MyComboBoxHandler myComboBoxHandler = new MyComboBoxHandler(); myComboBox.addItemListener( myComboBoxHandler ); myList = new JList(names); myList.setVisibleRowCount(3); myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); container.add(new JScrollPane(myList)); MyListHandler myListHandler = new MyListHandler(); myList.addListSelectionListener( myListHandler ); } 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())); } private class MyTextFieldHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog(null,"myTextField got it: " + event.getActionCommand() ); //myTextField.setText( "" ); ((JTextField)(event.getSource())).setText( "" ); } } private class MyButtonHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog(null,"myButton got it"); } } private class MyPasswordFieldHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog(null,"myPasswordField got it: " + event.getActionCommand()+ " oops" ); //myPasswordField.setText( "" ); ((JPasswordField)(event.getSource())).setText( "" ); } } private class MyCheckBoxHandler implements ItemListener { public void itemStateChanged( ItemEvent event ) { if ( event.getSource() == myCheckBox1 ) { if (event.getStateChange() == ItemEvent.SELECTED) JOptionPane.showMessageDialog(null,"myCheckBox 1 selected"); else if (event.getStateChange() == ItemEvent.DESELECTED) JOptionPane.showMessageDialog(null,"myCheckBox 1 deselected"); } else if ( event.getSource() == myCheckBox2 ) { if ( myCheckBox2.isSelected() ) //alternate way JOptionPane.showMessageDialog(null,"myCheckBox 2 selected"); else if (event.getStateChange() == ItemEvent.DESELECTED) JOptionPane.showMessageDialog(null,"myCheckBox 2 deselected"); } } } private class MyRadioButtonHandler implements ItemListener { public void itemStateChanged( ItemEvent event ) { if ( event.getSource() == myRadioButton1 ) { if (event.getStateChange() == ItemEvent.SELECTED) JOptionPane.showMessageDialog(null,"myRadioButton 1 selected"); else if (event.getStateChange() == ItemEvent.DESELECTED) JOptionPane.showMessageDialog(null,"myRadioButton 1 deselected"); } else if ( event.getSource() == myRadioButton2 ) { if ( myRadioButton2.isSelected() ) JOptionPane.showMessageDialog(null,"myRadioButton 2 selected"); else if (event.getStateChange() == ItemEvent.DESELECTED) JOptionPane.showMessageDialog(null,"myRadioButton 2 deselected"); } } } private class MyComboBoxHandler implements ItemListener { public void itemStateChanged( ItemEvent event ) { //this if not really needed... if ( event.getSource() == myComboBox ) 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()); } } private class MyListHandler implements ListSelectionListener { public void valueChanged( ListSelectionEvent event ) { if ( event.getSource() == myList ) JOptionPane.showMessageDialog(null,"myList index: " + myList.getSelectedIndex()); } } }