Most Sample Code from "Java How to Program, Fifth Edition" from Deitel and Deitel.


JApplet Review plus Changing to Application

Code from last week:
SquareIntegers2.html
SquareIntegers2.java

*Note: "implements ActionListener" which means that we are implementing an "interface" which requires the definition of the "actionPerformed" function.

Add components to the container by using the "add" function as in:

           container.add(myField);
          

Copy this applet into a file called "Application_E1.java" and turn it into an application by following these steps:

  1. Change "public class SquareIntegers2 extends..." to:
                     public class Application_E1 extends JFrame implements ActionListener
                   
  2. Since we are working with Applications, we need some way of exiting out of the Frames. We will add the following JButton after the class declaration:
                        JButton exit;
                   
  3. Change the "init()" function into a constructor function:
                        public Application_E1()
                   
  4. The first line to add in the constructor function calls the constructor function for JFrame:
                           super("Application Event Handling 1");
                   
  5. After creating our container, we can add an exit button:
                           exit = new JButton ("Exit");
                           container.add (exit);
                           exit.addActionListener (this); 
                   
  6. Inside the actionPerformed function, we add the following to handle the exit button:
                            else if (actionEvent.getSource()==exit)
                            {
                                  System.exit(0);
                            }
                   
  7. Also inside actionPerformed, change the two "showStatus..." lines to:
                                  Sytem.out.println("You hit the button");
                   
  8. Before the ending curly bracket, put the main function:
                        public static void main (String args[])
                        {
                            Application_E1 window = new Application_E1();
                            window.setSize(400,400);
                            window.setVisible(true);
                        }
                   

Resulting code:
Application_E1.java

This demonstrates one way of handling events. The following section demonstrates the different code one can use to implement event handling.


Three main ways of handling events

  1. As the primary class

    example: Application_E1.java

    indications of this method:

  2. As an Inner class

    example: Application_E2.java

    indications of this method:

  3. As an Anonymous Inner Class

    example: Application_E3.java

    indications of this method:

The following is meant to summarize what happens when you click a button:

        JButton (click)    --------->ActionEvent Object------>Processed by 
        OR JTextField (enter)        Created                  ActionListener's
                                                              actionPerformed
                                                              function

        
        JCheckBox                                             Processed by
        JRadioButton --------------->ItemEvent Object-------->ItemListener's
        OR JComboBox                 Created                  itemStateChanged
        selection                                             function
      

JCheckBox Buttons

Example code: CheckBoxTest.java

creating and adding checkboxes is simple:

              JCheckBox bold;
              bold = new JCheckBox ("Bold");
              container.add (bold);
          

Which of three above methods are we using to implement event handling?
method 2

*Notice that there is an inner class that implements ItemListener and defines the itemStateChanged function.


JRadioButton

Example code: RadioButtonTest.java

creating and adding radiobuttons:

              JRadioButton plainButton, boldButton, italicButton, boldItalicButton;
              ButtonGroup radio Group;
              ...
              plainButton = new JRadioButton ("Plain", true);
              container.add (plainButton);
    
              boldButton = new JRadioButton( "Bold", false );
              container.add( boldButton );

              italicButton = new JRadioButton( "Italic", false );
              container.add( italicButton );

              boldItalicButton = new JRadioButton( "Bold/Italic", false );
              container.add( boldItalicButton );

              // create logical relationship between JRadioButtons
              radioGroup = new ButtonGroup();
              radioGroup.add( plainButton );
              radioGroup.add( boldButton );
              radioGroup.add( italicButton );
              radioGroup.add( boldItalicButton );
       

Why do we need a Radio group?
because only one radio button is selected at a time.

Which method of event handling?
method 2 (have an inner class)

Notice in the code like the following
plainButton.addItemListener(new RadioButtonHandler(plainFont);
we are calling the constructor for the RadioButtonHandler and sending it a font argument. The four calls like this create four different ItemListeners (one for each button). Each ItemListener has the private font variable set appropriately for the corresponding button


JComboBox

Example code: ComboBoxTest.java
bug1.gif
bug2.gif
travelbug.gif
buganim.gif

creating and adding ComboBoxes:

             JComboBox imagesComboBox;
             String names[] = { "bug1.gif", "bug2.gif",  "travelbug.gif", 
                              "buganim.gif" };      

             imagesComboBox = new JComboBox( names );
             imagesComboBox.setMaximumRowCount( 3 );
             container.add(imagesComboBox);
         

Which method of event handling?
method 3


JPasswordField

Example code: TextFieldTest.java

creating and adding PasswordFields:

             JPasswordField passwordField;
   
             passwordField= new JPasswordField("Hidden text");
             container.add(passwordField);
         

Which method of event handling?
method 2


Exercise

Create a GUI interface that looks like this. When you click on "OK" a message box pops up.


Your interface may look more like this: