CIS 121 February 28, 2000


Recall that we have discussed several classes in the awt package and their associated methods. In the following tables we review some of these classes and part of the hierarchy of the awt package.




Component



Button Checkbox Choice Container
Label TextComponent



Window Panel TextArea
TextField



Frame





Object

Important Methods

Component public void setSize(int width,int height)
public void setBackground(Color c)
public void setVisible(boolean b)
public void setLocation(int x,int y)
public Graphics getGraphics()
Container public Component add(Component comp)
public void setLayout(LayoutManager mgr)
Window public synchronized void addWindowListener(WindowListener l)
Frame public synchronized void setMenuBar(MenuBar mb)
Button public synchronized void addActionListener(ActionListener l)
Constructors public Button()
public Button(String label)
Checkbox public synchronized void addItemListener(ItemListener l)
Constructors public Checkbox()
public Checkbox(String label)
public Checkbox(String label,CheckboxGroup group,boolean state)
TextField public synchronized void addActionListener(ActionListener l)
public synchronized void setText(String t) (from TextComponent)
public synchronized void getText() (from TextComponent)
Constructors public TextField()
public TextField(int columns)
public TextField(String text)
public TextField(String text,int columns
TextArea public synchronized void setText(String t) (from TextComponent)
public synchronized void getText() (from TextComponent)
Constructors public TextArea()
public TextArea(String text)
public TextArea(int rows,int columns)
public TextArea(String text,int rows,int columns)
public TextArea(String text,int rows,int columns,int scrollbars)
List public synchronized void addActionListener(ActionListener l)
public void add(String item)
Constructors public List()
public List(int rows,boolean multipleSelections)
Menu public synchronized MenuItem add(MenuItem mi)
public synchronized void addActionListener(ActionListener l) (from MenuItem)
Constructors public Menu()
public Menu(String label)
MenuBar public synchronized Menu add(Menu m)
Constructor public MenuBar()
Choice public synchronized void add(String item)
public synchronized void addItemListener(ItemListener l)
Constructor public Choice()
Graphics public abstract void clearRect(int x,int y,int width,int height)
public abstract void drawLine(int xl,int yl,int x2,int y2)
public abstract void drawOval(int x,int y,int width,int length)
public void drawRect(int x,int y,int width,int height)
public abstract void drawString(String str,int x,int y)
public abstract void fillOval(int x,int y,int width,int height)
public abstract void fillRect(int x,int y,int width,int height)
public abstract void setColor(Color c)
public abstract void setFont(Font font)
public String toString()

We have also discussed several interfaces. These interfaces are found in the java.awt.event package. In the following table we examine these interfaces and their methods.

Interface

Methods

ActionListener public abstract void actionPerformed(ActionEvent e)
ItemListener public abstract void itemStateChanged(ItemEvent e)
MouseListener public abstract void mouseClicked(MouseEvent e)
public abstract void mousePressed(MouseEvent e)
public abstract void mouseReleased(MouseEvent e)
public abstract void mouseEntered(MouseEvent e)
public abstract void mouseExited(MouseEvent e)
WindowListener public abstract void windowActivated(WindowEvent e)
public abstract void windowClosed(WindowEvent e)
public abstract void windowClosing(WindowEvent e)
public abstract void windowDeactivated(WindowEvent e)
public abstract void windowDeiconified(WindowEvent e)
public abstract void windowIconified(WindowEvent e)
public abstract void windowOpened(WindowEvent e)
TextListener public abstract void textValueChanged(TextEvent e)

In the following table lists some other classes that are important to us.

Object

Important Methods

Important Constants

ActionEvent
(from java.awt.event)
public String getActionCommand()
ItemEvent
(from java.awt.event)
public Object getItem()
public int getStateChange()
SELECTED
DESELECTED
EventObject
(from java.util)
public Object getSource()
MouseEvent
(from java.awt.event)
public int getX()
public int getY()
Color
(from java.awt)

black
blue
cyan
darkGray
gray
green
lightGray
magenta
orange
pink
red
white
yellow
Constructors public Color(float r,float g,float b)
public Color(int rgb)
public Color(int r,int g,int b)

Font
(from java.awt)

BOLD
ITALIC
name
PLAIN
size
style
Constructor public Font(String name,int style,int size)


An important issue to consider is how components are layed out in a container. Java offers us 5 different layout managers. The two we consider are the FlowLayout and GridLayout.

Object

Constructors

FlowLayout public FlowLayout()
public FlowLayout(int align)
public FlowLayout(int align,int hgap,int vgap)
GridLayout public GridLayout()
public GridLayout(int rows,int columns)
public GridLayout(int rows,int columns,int hgap,int vgap)

If we want to control the appearance of the container, then we can choose not to use one of the 5 layout managers. We still make a call to setLayout method but we send the keyword null as a parameter. We must use both the setSize and setLocation methods in order for the components to be displayed. It may take some experimentation to find the correct coordinates for our components. Remember that the upper left-hand corner of the container has coordinates (0,0), and the coordinates of our components are the coordinates of the upper left-hand corner of our component. Recall that the units are pixels.
Procedure to detect and act on a Button click
  1. Create a Button object, button, with buttonLabel.
  2. Add an actionListener with the command button.addActionListener(this).
  3. Implement the actionPerformed method from the ActionListener interface.
  4. In the body of actionPerformed use either getSource or getActionCommand.
  5. getSource returns an Object, and getActionCommand returns the text associated with the Button.
  6. Examples:
        public void actionPerformed(ActionEvent event) {
           if (event.getSource() == button) {
              ...
           }
        }
    
        public void actionPerformed(ActionEvent event) {
           if (event.getActionCommand().equals(buttonLabel)) {
              ...
           }
        }
        
Procedure to detect and act on a Checkbox event
  1. Create a Checkbox object,c, with label checkBoxLabel.
  2. Add an itemListener with the command c.addItemListener(this).
  3. Implement the itemStateChanged method from the ItemListener interface.
  4. In the body of itemStateChanged, use getItem to detect which checkbox caused the event.
  5. Example:
        public void itemStateChanged(ItemEvent event) {
           if (event.getStateChange() == ItemEvent.SELECTED) {
              if (event.getItem() == checkBoxLabel) {
                 ...
              }
           }
        }
        
Procedure to detect and act on a TextField event
  1. Create a TextField object,textField.
  2. Add an actionListener with the command textField.addActionListener(this).
  3. Implement the actionPerformed method from the ActionListener interface.
  4. In the body of actionPerformed, use getActionCommand.
  5. Example:
        public void actionPerformed(ActionEvent event) {
           System.out.println(event.getActionCommand());
        }
        
Note: A TextField event is triggered by hitting the enter key.

Procedure to detect and act on a List event
  1. Create a List object,list.
  2. Use the add method from the List class to add items to the list.
  3. Add both an ActionListener and an ItemListener because a list can generate two types of events.
  4. An ActionEvent is generated if you double-click on an item.
  5. When we click on an item an ItemEvent is created. We can use the getItem method from ItemEvent to get the item that created the event. Since the return type from getItem is an Object, we need to cast it to an integer. 0 represents the first item in the list, 1 represents the second item in the list, etc.
  6. Example:
        public void itemStateChanged(ItemEvent event) {
           position = ((Integer)event.getItem()).intValue();
        }
        
        public void actionPerformed(ActionEvent event) {
           nameOfItem = event.getActionCommand();
        }
        
Procedure to detect a Menu event
  1. Create a new Menubar object,menubar.
  2. Create new Menu objects.
  3. Use the add method from the Menu class to add items to the menus.
  4. Use the add method from the MenuBar class to add the menus to the menubar.
  5. Add an actionListener to the menus with the addActionListener method.
  6. Add the menubar to the container with the setMenuBar method from the Frame class.
  7. Implement the actionPerformed method from the ActionListener interface.
  8. Example:
        public void actionPerformed(ActionEvent event) {
           System.out.println(event.getActionCommand());
        }
        
An important issue to consider is how components are layed out in a container. Java offers us 5 different layout managers. The two we consider are the FlowLayout and GridLayout.

Object

Constructors

FlowLayout public FlowLayout()
public FlowLayout(int align)
public FlowLayout(int align,int hgap,int vgap
GridLayout public GridLayout()
public GridLayout(int rows,int columns)
public GridLayout(int rows,int columns,int hgap,int vgap)

Examples


A program to demonstrate capturing a button click and based on that button click placing text into a textfield

ButtonController.java
import java.awt.*;
import java.awt.event.*;

public class ButtonController extends Frame implements ActionListener,WindowListener {
   Button myButton1;
   Button myButton2;
   TextField myTextField;

   public ButtonController(String s) {
      super(s);

      setLayout(null);

      myButton1 = new Button("Print Message");
      myButton2 = new Button("Erase Message");
      myTextField = new TextField();

      myButton1.setLocation(140,150);
      myButton1.setSize(100,20);

      myButton2.setLocation(260,150);
      myButton2.setSize(100,20);

      myTextField.setLocation(200,50);
      myTextField.setSize(100,20);

      add(myButton1);
      add(myButton2);
      add(myTextField);

      myButton1.addActionListener(this);
      myButton2.addActionListener(this);

      myTextField.addActionListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Print Message")) {
         myTextField.setText("Example");
      }
      if (e.getSource() == myButton2) {
         myTextField.setText("");
      } 
   }

}
MyGui.java
public class MyGui {

   public static void main(String[] args) {
      ButtonController myController = new ButtonController("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}

A program to demonstrate capturing a list selection and based on that selection writing text into a textfield

ListController.java
import java.awt.*;
import java.awt.event.*;

public class ListController extends Frame implements ActionListener,ItemListener,WindowListener {
   List myList;
   TextField myTextField;

   public ListController(String s) {
      super(s);

      setLayout(null);

      myList = new List(1,false);
      myTextField = new TextField();

      myList.add("Print Message");
      myList.add("Erase Message");

      myList.setLocation(200,150);
      myList.setSize(100,40);

      myTextField.setLocation(200,50);
      myTextField.setSize(100,20);

      add(myList);
      add(myTextField);

      myList.addActionListener(this);
      myList.addItemListener(this);
      myTextField.addActionListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void itemStateChanged(ItemEvent e) {
      int position = ((Integer)e.getItem()).intValue();

      if (position == 0) {
         myTextField.setText("Example");
      }
      if (position == 1) {
         myTextField.setText("");
      }
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Print Message")) {
         myTextField.setText("Example");
      }
      if (e.getActionCommand().equals("")) {
         myTextField.setText("");
      }   
   }

}
MyGui1.java
public class MyGui1 {

   public static void main(String[] args) {
      ListController myController = new ListController("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}

A program to demonstrate capturing a radio button selection and based on that selection writing text into a textfield

CheckboxController.java
import java.awt.*;
import java.awt.event.*;

public class CheckboxController extends Frame implements ItemListener,WindowListener {
   Checkbox myCheckbox1;
   Checkbox myCheckbox2;
   TextField myTextField;

   public CheckboxController(String s) {
      super(s);

      setLayout(null);

      CheckboxGroup choices = new CheckboxGroup();

      myCheckbox1 = new Checkbox("Print Message",choices,true);
      myCheckbox2 = new Checkbox("Erase Message",choices,false);

      myTextField = new TextField();

      myCheckbox1.setLocation(120,150);
      myCheckbox1.setSize(150,40);

      myCheckbox2.setLocation(300,150);
      myCheckbox2.setSize(150,40);

      myTextField.setLocation(200,50);
      myTextField.setSize(100,20);

      add(myCheckbox1);
      add(myCheckbox2);

      add(myTextField);

      myCheckbox1.addItemListener(this);
      myCheckbox2.addItemListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void itemStateChanged(ItemEvent e) {
      if (e.getStateChange() == ItemEvent.SELECTED) {   
         if (e.getItem() == "Print Message") {
            myTextField.setText("Example");
         }
         if (e.getItem() == "Erase Message") {
            myTextField.setText("");
         }
      }
   }

}
MyGui2.java
public class MyGui2 {

   public static void main(String[] args) {
      CheckboxController myController = new CheckboxController("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}

A program to demonstrate creating a MenuBar with a Menu and based on the selection writing text into a textfield

MenuController.java
import java.awt.*;
import java.awt.event.*;

public class MenuController extends Frame implements ActionListener,WindowListener {
   MenuBar myMenuBar;
   Menu options;
   TextField myTextField;

   public MenuController(String s) {
      super(s);

      setLayout(null);

      CheckboxGroup choices = new CheckboxGroup();

      myMenuBar = new MenuBar();

      options = new Menu("Options");

      options.add("Print Message");
      options.add("Erase Message");

      myMenuBar.add(options);

      this.setMenuBar(myMenuBar);

      myTextField = new TextField();

      myTextField.setLocation(200,50);
      myTextField.setSize(100,20);

      add(myTextField);

      options.addActionListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Print Message")) {
         myTextField.setText("Example");
      }
      if (e.getActionCommand().equals("Erase Message")) {
         myTextField.setText("");
      }
   }

}
MyGui3.java
public class MyGui3 {

   public static void main(String[] args) {
      MenuController myController = new MenuController("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}

A program using panels to accomplish the same thing that was done with the ButtonController

Viewer.java
import java.awt.*;
import java.awt.event.*;

public class Viewer extends Frame implements ActionListener,WindowListener {
   Button myButton1;
   Button myButton2;
   TextField myTextField;

   public Viewer(String s) {
      super(s);

      setLayout(null);

      Panel panel1 = new Panel(new FlowLayout());
      Panel panel2 = new Panel(new FlowLayout());

      myButton1 = new Button("Print Message");
      myButton2 = new Button("Erase Message");

      myTextField = new TextField(20);

      panel1.setSize(500,100);
      panel2.setSize(500,100);

      panel1.add(myTextField);

      panel2.add(myButton1);
      panel2.add(myButton2);

      panel1.setLocation(0,20);
      panel2.setLocation(0,120);

      panel2.setBackground(Color.yellow);

      add(panel1);
      add(panel2);

      myButton1.addActionListener(this);
      myButton2.addActionListener(this);

      addWindowListener(this);

   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Print Message")) {
         myTextField.setText("Example");
      }
      if (e.getSource() == myButton2) {
         myTextField.setText("");
      } 
   }

}
MyGui4.java
public class MyGui4 {

   public static void main(String[] args) {
      Viewer myController = new Viewer("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}

A program which will listen for a mouse click, get the x and y coordinates of where the mouse was clicked, and display the coordinates in a TextField

MouseTester.java
import java.awt.event.*;

class MouseTester extends Frame implements MouseListener,WindowListener {
   TextField myTextField;

   public MouseTester(String s) {
      super(s);
      setBackground(Color.yellow);

      setLayout(new FlowLayout());

      myTextField = new TextField(20);

      add(myTextField);

      addMouseListener(this);
      addWindowListener(this);
   }

   public void mouseClicked(MouseEvent e) { }
   public void mouseReleased(MouseEvent e) { }
   public void mouseEntered(MouseEvent e) { }
   public void mouseExited(MouseEvent e) { }

   public void mousePressed(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();

      myTextField.setText("[" + String.valueOf(x) + "," + String.valueOf(y) + "]");
   }

   public void windowActivated(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowDeactivated(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      System.exit(0);
   }

}
MyGui5.java
public class MyGui5 {

   public static void main(String[] args) {
      MouseTester myController = new MouseTester("Viewer");

      myController.setSize(500,200);
      myController.setVisible(true);    
   }

}