// SwingGUIsFrameDemo.java // One each of several Swing GUI widgets in a JFrame. /* Using an interactive GUI widget: 1. declare and instantiate it. optionally, configure it your needs. 2. add it to a container. 3. specify where its handler code is, i.e. "register" the handler. 4. put the handler code in the required method for the widget Each widget has a handler method that responds to user using it (e.g. push button, check checkbox, input textfield, select list item etc.) In this demo the event handlers are 'this' object (this is the simplest technique), meaning this object has the handlers (the handler methods are in this class). The handlers are the methods you must have in order to respond to use of the widgets; they are the required methods for implementing the various interfaces. JTextField, JPasswordField, JButton event handler is actionPerformed() which is the method required to implement ActionListener. JCheckBox, JRadioButton, JComboBox event handler is itemStateChanged() which is the method required to implement ItemListener. JList event handler is valueChanged() which is the method required to implement ListSelectionListener. When a JButton object is pressed by user it sends an event to the listener that has been registered to handle (deal with) that event. The listener/handler can do anything in response. The ability to send the event is built-in to the button. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class SwingGUIsFrameDemo implements ActionListener, ItemListener, ListSelectionListener { //declare as instance variables so the various methods can access them private JFrame outputFrame; private Container container; 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 static void main( String [] args ) { //start a frame object. call constructor. SwingGUIsFrameDemo myFrameDemo = new SwingGUIsFrameDemo(); //a real application would do other initializations... //then wait until user interacts with widgets. This is //event-driven program(ming) where it's the events that trigger responses. //Instead of an input statement waiting for input, there's no place in the code //where the program is at while it waits for user to cause an event. } //constructor public SwingGUIsFrameDemo() { outputFrame = new JFrame(); outputFrame.setSize(600,500); outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); container = outputFrame.getContentPane(); //not using a JPanel. widgets to be added directly to frame's content pane. container.setLayout( new FlowLayout() ); //not so pretty or ordered but is simple //container.setLayout( new GridLayout(8,2) ); //regular, but ugly container.setBackground(Color.CYAN); //otherwise is gray //instantiate a JLabel object, add it to container myLabel = new JLabel( "My JLabel" ); container.add( myLabel ); myTextField = new JTextField( "My JTextField", 20 ); //optional size container.add( myTextField ); //myTextField.setEditable( false ); //default is editable. //say where the handler is. right here in 'this' SwingGUIsFrameDemo object. //handler is the actionPerformed() below. i.e. when user Enters text into this //textfield widget, the actionPerformed() method is executed to handle that event. myTextField.addActionListener( this ); myPasswordField = new JPasswordField( "Type your password here",20 ); container.add( myPasswordField ); //***handler is the actionPerformed() below. myPasswordField.addActionListener( this ); myButton = new JButton( "My JButton" ); container.add( myButton ); //handler is the actionPerformed() below. 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 ); //handler is the itemStateChanged() below. 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 ); //handler is the itemStateChanged() below. 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); //array of Strings has the combobox strings 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)); //attach a scrollbar //container.add(myList); //otherwise no scrollbar //***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)); //won't have a handler. Any code can write into the textarea. outputFrame.setVisible(true); } // event handler for JButton, JTextField and JPasswordField events. //required method if implementing ActionListener public void actionPerformed( ActionEvent event ) { //***if several possible widgets, need to determine which one had the event //that invoked this method: if ( event.getSource() == myButton ) { JOptionPane.showMessageDialog(null,"myButton got it"); myTextArea.append("myButton pressed\n"); } else if ( event.getSource() == myTextField ) { //2 ways to get the text of the textfield: JOptionPane.showMessageDialog(null,"myTextField got it one way: " + event.getActionCommand() ); JOptionPane.showMessageDialog(null,"myTextField got it the other way: " + myTextField.getText() ); //***access the text myTextArea.append("myTextField: " + myTextField.getText() + "\n"); myTextField.setText( "" ); //***change the text } 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. //required method if implementing ItemListener 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 to see which event happened 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() ) { JOptionPane.showMessageDialog(null,"myRadioButton 1 selected"); myTextArea.append("myRadioButton 1 selected\n"); } } else if ( event.getSource() == myRadioButton2 ) { if ( myRadioButton2.isSelected() ) { 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(); //index of combobox items 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. //required method if implementing ListSelectionListener public void valueChanged( ListSelectionEvent event ) { if ( event.getSource() == myList ) { int i = myList.getSelectedIndex(); //similar to combobox JOptionPane.showMessageDialog(null,"myList index: " + i + "\nitem value: " + names[i]); myTextArea.append("myList selected index: " + i + " item value: " + names[i] + "\n"); } } }