GUI Components and Event Handling (in Applet)


GUI Components

Label
Label noNameLabel = new Label(); 

Label roadLabel = new Label("One Way"); 

noNameLabel.setText("Two Way");          

add(noNameLabel);
add(roadLabel);
Button
Button noNameButton = new Button(); 
Button startButton = new Button("Start");

String s = startButton.getLabel(); 

Button stopButton = new Button();
stopButton.setLabel("Stop");

add(noNameButton);
add(startButton);
add(stopButton);
TextField
TextField f1 = new TextField();
TextField f2 = new TextField();
TextField f3 = new TextField(15);
TextField f4 = new TextField("Hello");
TextField f5 = new TextField("How are you",25);
TextField f6 = new TextField(10);

f2.setText("Good morning");

// echo * whatever you input
f6.setEchoChar('*'); 

// passwd = whatever you input      
String passwd = f6.getText(); 

add(f1); add(f2);
add(f3); add(f4);
add(f5); add(f6);
TextArea
TextArea t1 = new TextArea(2,10); 
TextArea t2 = new TextArea("Hello",4,20); 

add(t1);
add(t2);
List
List years = new List();
years.add("2007");
years.add("2008");
years.add("2009");
years.add("2010");
years.add("2011");
years.add("2012");
years.add("2013");
years.add("2014");
years.add("2015");

add(years);
Scrollbar
// vertical scrollbar
Scrollbar vscrollbar = new Scrollbar();

// horizontal scrollbar
Scrollbar hscrollbar = 
   new Scrollbar(Scrollbar.HORIZONTAL); 

hscrollbar.setUnitIncrement(20);
hscrollbar.setMinimum(0);
hscrollbar.setMaximum(100);

add(vscrollbar);
add(hscrollbar);
Choice
Choice threeChoices = new Choice();
add(threeChoices);

threeChoices.add("Choice 1");
threeChoices.add("Choice 2");
threeChoices.add("Choice 3");

threeChoices.select("Choice 2");
String s = threeChoices.getSelectedItem(); 

Checkbox
Checkbox checkbox1 = new Checkbox();  // no name

Checkbox checkbox2 = new Checkbox("Any Time");

Checkbox checkbox3 = new Checkbox("Non-stop", true);  

add(checkbox1);
add(checkbox2);
add(checkbox3);
CheckboxGroup
CheckboxGroup TV = new CheckboxGroup();

Checkbox  c1 = new Checkbox("ABC 7", TV, false);
Checkbox  c2 = new Checkbox("FOX 11", TV, false);
Checkbox  c3 = new Checkbox("KRWG 22", TV, true);

add(c1);
add(c2);
add(c3);
Panel
Panel p1 = new Panel();
p1.add(new Button("Button in p1"));

Panel p2 = new Panel();
p2.add(new Button("Button in p2"));

p1.add(p2);

add(p1);


Questions:


GUI Layout Demo

FlowLayout
Place the components from left to right. If the border of the window is hit, place in the next row and from left to right.
FlowLayout layout = new FlowLayout(); 
setLayout(layout);
add(new Button("Start"));
add(new Button("Stop"));
add(new Button("Cut"));
add(new Button("Paste"));
add(new Button("Copy"));
add(new Button("Clear"));
  
BorderLayout
The window is divided into 5 areas named East, West, North, South and Center. You specify the area where a component should be placed while invoking the add method.
setLayout(new BorderLayout());
add("North", new Label("Title area"));
add("South", new Label("Footnote area"));
add("East", new Button("Right"));
add("West", new Button("Left"));
add("Center", new TextArea("hello, textarea"));
  
GridLayout
The window is divided into M rows and N colums evenly. The placement of the components is row by row and from left to right.
// 3 rows and 3 columns  with gaps of size 5
setLayout( new GridLayout(3,3,5,5) );
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
add(new Button("7"));
add(new Button("8"));
  
Insets
  
Skip
CardLayout
  
Skip


Questions:


Event Handling

The java.awt.event package




Table 1: Events, Listeners and add/remove methods

Event Listener add/remove methods
ActionEvent
(a component-defined action occurred, e.g. a button pressed)
ActionListener addActionListener(), removeActionListener()
AdjustmentEvent
(the current value of an Adjustable object is changed)
Listener addAdjustListener(), removeAdjustListener()
ComponentEvent
(a component moved, changed size, or changed visibility)
ComponentListener addComponentListener(), removeComponentListener()
ContainerEvent
(a component added to or removed from the container )
ContainerListener addContainerListener(), removeContainerListener()
FocusEvent
(a Component has gained or lost the input focus)
FocusListener addFocusListener(), removeFocusListener()
KeyEvent
(keystroke occurred in a component)
KeyListener addKeyListener(), removeKeyListener()
MouseEvent
(mouse action occurred in a component)
MouseListener addMouseListener(), removeMouseListener()
MouseEvent MouseMotionListener addMouseMotionListener(), removeMouseMotionListener()
WindowEvent
(a window has changed its status)
WindowListener addWindowListener(), removeWindowListener()
ItemEvent
(an item was selected or deselected)
ItemListener addItemListener(), removeItemListener()
TextEvent
(an object's text changed)
TextListener addTextListener(), removeTextListener()


Table 2: Components and their supported Events

Component Type Events supported
Adjustable AdjustmentEvent
Applet ContainerEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
Button ActionEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
Canvas FocusEvent,KeyEvent,MouseEvent,ComponentEvent
Checkbox ItemEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
Choice ItemEvent,FocusEvent,KeyEvent,MouseEbent,ComponentEvent
Component FocusEvent,KeyEvent,MouseEvent,ComponentEvent
Container ContainerEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
Frame ContainerEvent,WindowEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
Label FocusEvent,KeyEvent,MouseEvent,ComponentEvent
List ActionEvent,FocusEvent,KeyEvent,MouseEvent,ItemEvent
Menu ActionEvent
MenuItem ActionEvent
Panel ContainerEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
PopupMenu ActionEvent
Scrollbar AdjustmentEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
TextArea TextEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
TextComponent TextEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
TextField ActionEvent,TextEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent
Window ContainerEvent,WindowEvent,FocusEvent,KeyEvent,MouseEvent,ComponentEvent


Table 3: Listener Interfaces

Listener Methods in interface
actionListerner actionPerformed(ActionEvent)
AdjustmentListener adjustmentValueChanged(AdjustmentEvent)
ComponentListener
ComponentAdapter
componentHidden(ComponentEvent)
componentShown(ComponentEvent)
componentMoved(ComponentEvent)
componentResized(ComponentEvent)
ContainerListener
ContainerAdapter
componentAdded(ContainerEvent)
componentRemoved(ContainerEvent)
FocusListener
FocusAdapter
focusGained(FocusEvent)
focusLost(FocusEvent)
KeyListener
KeyAdapter
keyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTyped(KeyEvent)
MouseListener
MouseAdapter
mouseClicked(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
MouseMotionListener
MouseMotionAdapter
mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
WindowListener
WindowAdapter
windowOpened(WindowEvent)
windowClosing(WindowEvent)
windowClosed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
ItemListener itemStateChanged(ItemEvent)
TextListener textValueChanged(TextEvent)


In order to implement a listener interface, you need to implement all the declared methods in the interface. Even though you may not need all of them to do useful things, you still need to provide an implementation for each method, some implementations being such that they do nothing. This is inconvenient for the programmer.

The adapter class (e.g., MouseAdapter class) of an listener interface (e.g., MouseListener) extends the interface by providing a default implementation. If you extends the adapter class rather than implements the listener interface, you only need to define the methods you are interested in, without having to providing an implementation for each method.


Example: ButtonEventDemo.java (Approach #1: Using an inner class)

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class ButtonEventDemo extends Applet {

   Button button1, button2;
   TextField textfield;

   class MyButtonEventListener implements ActionListener {

       public void actionPerformed(ActionEvent e) {

           if (e.getSource().equals(button1)) {
               textfield.setText("   somebody pressed the button");
           } 
           else if (e.getSource().equals(button2)) {
               
               textfield.setText("");
           }
       }
   }

   public void init () {

       button1 = new Button("Press me");
       button2 = new Button("Clear");
       textfield = new TextField(30);       
    
       MyButtonEventListener listener 
            = new MyButtonEventListener();

       button1.addActionListener(listener);
       button2.addActionListener(listener);

       add(button1);
       add(button2);
       add(textfield);
   }
}


Note:



Example: ButtonEventDemo.java (Approach #2: Let your class implement the listener interface)
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class ButtonEventDemo extends Applet implements ActionListener {

   Button button1, button2;
   TextField textfield;


   public void actionPerformed(ActionEvent e) {

           if (e.getSource().equals(button1)) {
               textfield.setText("   somebody pressed the button");
           } 
           else if (e.getSource().equals(button2)) {
               
               textfield.setText("");
           }
   }

   public void init () {

       button1 = new Button("Press me");
       button2 = new Button("Clear");
       textfield = new TextField(30);       
    
       button1.addActionListener(this);
       button2.addActionListener(this);

       add(button1);
       add(button2);
       add(textfield);
   }
}


Example: ButtonEventDemo.java (Approach #3: Generate a listener in fly)
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class ButtonEventDemo3 extends Applet {

   Button button1, button2;
   TextField textfield;

   public void init () {

       button1 = new Button("Press me");
       button2 = new Button("Clear");
       textfield = new TextField(30);       
    
       button1.addActionListener(
           new ActionListener() 
           {
                public void actionPerformed(ActionEvent e) {

                   textfield.setText("   somebody pressed the button");

                }
           }
       );

       button2.addActionListener(
           new ActionListener() 
           {
                public void actionPerformed(ActionEvent e) {

                   textfield.setText("");

                }
           }

       );

       add(button1);
       add(button2);
       add(textfield);
   }
}