Java Lecture 6

[Previous Lecture] [Lecture Index] [Next Lecture]

Frequently used Listeners/Events

ActionListener:

generated by components when they are `activated' (e.g., Button, MenuItem, List, TextField, etc.)

ItemListener:

generated by components that allow items to be selected (e.g., Choice, Checkbox, List, etc.)

MouseListener:

MouseMotionListener:

KeyListener:


TextFieldDemo.java

/*
 * <applet code=TextFieldDemo width=200 height=200>
 * </applet>
 */
import java.awt.*;
import java.awt.event.*;

public class TextFieldDemo
        extends java.applet.Applet
        implements ActionListener
{
    TextField t;

    public void init() {
        t = new TextField();
        t.addActionListener(this);
        t.setText("Maybe");

        add(new Label("Answer: "));
        add(t);

    }
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == t) {
            System.out.println("Answer is: " + t.getText());
            System.out.println("same as: " + e.getActionCommand());
        }
        return;
    }
}

ChoiceDemo.java

/*
 * <applet code=ChoiceDemo width=200 height=200>
 * </applet>
 */
import java.awt.*;
import java.awt.event.*;

public class ChoiceDemo
        extends java.applet.Applet
        implements ItemListener
{
    Choice c;

    public void init() {
        c = new Choice();
        c.addItemListener(this);

        c.add("Yes");
        c.add("No");
        c.add("Maybe");
        add(new Label("Answer: "));
        add(c);

    }
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == c) {
            System.out.println("Answer is: " + c.getSelectedItem());
            System.out.println("same as: " + e.getItem());
        }
        return;
    }
}

CheckboxDemo.java

/*
 * <applet code=CheckboxDemo width=200 height=200>
 * </applet>
 */
import java.awt.*;
import java.awt.event.*;

public class CheckboxDemo
        extends java.applet.Applet
        implements ItemListener
{
    Checkbox c1, c2;
    Checkbox cg1, cg2, cg3, cg4;
    CheckboxGroup myGroup;

    public void init() {
        c1 = new Checkbox("Box 1");
        c1.addItemListener(this);
        c2 = new Checkbox("Box 2", null, true);
        c2.addItemListener(new ItemListener() {
          public void itemStateChanged( ItemEvent e) {
              if (e.getStateChange() == ItemEvent.SELECTED)
                  System.out.println("Box 2 selected");
          }
         });
        add(c1);
        add(c2);

        myGroup = new CheckboxGroup();
        cg1 = new Checkbox("Option 1");
        cg1.setCheckboxGroup(myGroup);
        cg1.addItemListener(this);
        cg2 = new Checkbox("Option 2", myGroup, false);
        cg2.addItemListener(this);
        cg3 = new Checkbox("Option 3", myGroup, true);
        cg3.addItemListener(this);
        cg4 = new Checkbox("Option 4", myGroup, true);
        cg4.addItemListener(this);
        Panel p = new Panel();
        p.setLayout(new GridLayout(2, 2));
        p.add(cg1);
        p.add(cg2);
        p.add(cg3);
        p.add(cg4);
        add(p);
    }

    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == c1) {
            if (e.getStateChange() == ItemEvent.SELECTED)
                System.out.println("Box one selected");
            else
                System.out.println("Box one de-selected");
            return;
        }
        if (e.getSource() instanceof Checkbox){
            Checkbox target = (Checkbox) e.getSource();

            if (target.getCheckboxGroup() == myGroup)
            {
            // target is always equal to
            //   myGroup.getSelectedCheckbox()
                System.out.println( "Current option: "
                        + target.getLabel());
                return;
            }
        }
        return;
    }
}

FrameDemo.java

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

public class FrameDemo
        extends Frame
        implements ActionListener
{
  public static void main(String args[]) {
    FrameDemo f = new FrameDemo();
    f.pack(); // layout components
    f.show(); // display on screen
  }

  List l;

  public FrameDemo() {
    super("The Frame test");

    l = new List();
    l.addActionListener(this);

    l.add("abc");
    l.add("defg");
    l.add("hijk");
    add(new Label("Select one: "));
    add(l);
  }


  public void actionPerformed(ActionEvent ae) {
      if (ae.getSource() == l) {
          System.out.println("Selected string:"
                + ae.getActionCommand());
          System.out.println("Selected index:"
                + l.getSelectedIndex());
    }
  }
}

Overview of Components

Canvas

Label

Button

Choice

Checkbox

List

TextField

TextArea


Overview of Layout Managers

FlowLayout

BorderLayout

GridLayout

CardLayout


Graphics Class

Some selected methods:
[Previous Lecture] [Lecture Index] [Next Lecture]