// SwingGUIs.java // One of each Swing GUI in JApplet with FlowLayout // Event handlers are this import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class SwingGUIs extends JApplet implements ActionListener, ItemListener, ListSelectionListener { private JLabel myLabel; private JTextField myTextField; private JPasswordField myPasswordField; private JButton myButton, myButton2; private JCheckBox myCheckBox1, myCheckBox2; private JRadioButton myRadioButton1, myRadioButton2; private ButtonGroup myButtonGroup; private JComboBox myComboBox; private JList myList; private JTextArea myTextArea; String [] names = {"fred", "jane", "steve", "susan", "bob"}; public void init() { // get applet's content pane and change its layout to FlowLayout Container container = getContentPane(); container.setLayout( new FlowLayout() ); container.setBackground(Color.MAGENTA); myLabel = new JLabel( "My JLabel" ); container.add( myLabel ); myTextField = new JTextField( "My JTextField", 20 ); container.add( myTextField ); //myTextField.setEditable( false ); myTextField.addActionListener( this ); myPasswordField = new JPasswordField( "Type your password here",20 ); container.add( myPasswordField ); myPasswordField.addActionListener( this ); myButton = new JButton( "My JButton" ); container.add( myButton ); myButton.addActionListener( this ); myCheckBox1 = new JCheckBox( "My JCheckBox 1" ); container.add( myCheckBox1 ); //***handler is the itemStateChanged() below. myCheckBox1.addItemListener( this ); myCheckBox2 = new JCheckBox( "My JCheckBox 2", true ); container.add( myCheckBox2 ); myCheckBox2.addItemListener( this ); myRadioButton1 = new JRadioButton( "My JRadioButton 1" ); container.add( myRadioButton1 ); //***handler is the itemStateChanged() below. myRadioButton1.addItemListener( this ); myRadioButton2 = new JRadioButton( "My JRadioButton 2" ); container.add( myRadioButton2 ); myRadioButton2.addItemListener( this ); //ButtonGroup makes radio buttons act as a group of radio buttons myButtonGroup = new ButtonGroup(); myButtonGroup.add(myRadioButton1); myButtonGroup.add(myRadioButton2); //ButtonGroup NOT added to container //ButtonGroup does not have a handler myComboBox = new JComboBox(names); myComboBox.setMaximumRowCount(3); container.add( myComboBox ); //handler is the itemStateChanged() below. myComboBox.addItemListener( this ); myList = new JList(names); myList.setVisibleRowCount(3); myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); container.add(new JScrollPane(myList)); //container.add(myList); //***handler is the valueChanged() below. myList.addListSelectionListener( this ); myButton2 = new JButton( "My useless JButton" ); container.add( myButton2, 6 ); //insert into position 6 myButton2.addActionListener( this ); //but the handler ignores it so nothing will happen when clicked. //create a textarea, attach a scrollbar, add to container. //textarea is like DOS box and println. myTextArea = new JTextArea(10,40); //rows,columns container.add( new JScrollPane(myTextArea)); } 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())); } // event handler for JButton, JTextField and JPasswordField events public void actionPerformed( ActionEvent event ) { if ( event.getSource() == myButton ) { JOptionPane.showMessageDialog(null,"myButton got it"); myTextArea.append("myButton pressed\n"); } else if ( event.getSource() == myTextField ) { JOptionPane.showMessageDialog(null,"myTextField got it one way: " + event.getActionCommand() ); JOptionPane.showMessageDialog(null,"myTextField got it other way: " + myTextField.getText() ); myTextArea.append("myTextField: " + myTextField.getText() + "\n"); //myTextField.setText( "" ); } else if ( event.getSource() == myPasswordField ) { JOptionPane.showMessageDialog(null,"myPasswordField got it: " + event.getActionCommand()+ " oops" ); myTextArea.append("myPasswordField entered\n"); //myPasswordField.setText( "" ); } } // event handler for JCheckBox, JRadioButton and JComboBox events public void itemStateChanged( ItemEvent event ) { if ( event.getSource() == myCheckBox1 ) { if (event.getStateChange() == ItemEvent.SELECTED) { JOptionPane.showMessageDialog(null,"myCheckBox 1 selected"); myTextArea.append("myCheckBox 1 selected\n"); } else if (event.getStateChange() == ItemEvent.DESELECTED) { JOptionPane.showMessageDialog(null,"myCheckBox 1 deselected"); myTextArea.append("myCheckBox 1 deselected\n"); } } else if ( event.getSource() == myCheckBox2 ) { if ( myCheckBox2.isSelected() ) { //alternate way JOptionPane.showMessageDialog(null,"myCheckBox 2 selected"); myTextArea.append("myCheckBox 2 selected\n"); } else if ( ! myCheckBox2.isSelected() ) { JOptionPane.showMessageDialog(null,"myCheckBox 2 deselected"); myTextArea.append("myCheckBox 2 deselected\n"); } } else if ( event.getSource() == myRadioButton1 ) { if ( myRadioButton1.isSelected() ) { //alternate way JOptionPane.showMessageDialog(null,"myRadioButton 1 selected"); myTextArea.append("myRadioButton 1 selected\n"); } } else if ( event.getSource() == myRadioButton2 ) { if ( myRadioButton2.isSelected()) { //alternate way JOptionPane.showMessageDialog(null,"myRadioButton 2 selected"); myTextArea.append("myRadioButton 2 selected\n"); } } else if ( event.getSource() == myComboBox ) { if ( event.getStateChange() == ItemEvent.SELECTED ) { int i = myComboBox.getSelectedIndex(); JOptionPane.showMessageDialog(null,"myComboBox selected index:" + i + "\nitem value: " + names[i]); myTextArea.append("myComboBox selected index: " + i + " item value: " + names[i] + "\n"); } } } //event handler for JList public void valueChanged( ListSelectionEvent event ) { if ( event.getSource() == myList ) { int i = myList.getSelectedIndex(); JOptionPane.showMessageDialog(null,"myList index: " + i + "\nitem value: " + names[i]); myTextArea.append("myList selected index: " + i + " item value: " + names[i] + "\n"); } } }