Web Note (9-10)
Web Note (9-10)
Applet Skeleton
A skeleton for an applet includes defining a class that extends Applet and overriding its lifecycle methods (like init(), start(),
stop(), and destroy()).
Basic Applet Skeleton:
import java.applet.Applet;
import java.awt.Graphics;
Important Notes:
• Security: Applets run in a restricted environment, known as a sandbox, which limits their access to system
resources.
• Deprecated: Applets are largely deprecated in modern browsers due to security concerns and the rise of other
technologies such as JavaScript and HTML5. As of 2015, most modern browsers no longer support Java applets.
2. Event Classes
Java provides different event classes to represent various events. These classes contain the information about the event and
are used to pass this information from the source to the listener.
• ActionEvent: Represents events like button clicks, menu selections, etc.
• MouseEvent: Represents mouse events such as clicks, mouse movement, etc.
• KeyEvent: Represents keyboard events (e.g., key presses and releases).
• WindowEvent: Represents events like window opening, closing, etc.
• ItemEvent: Represents item events, such as selecting a check box or a radio button.
3. Sources of Events
The source of an event is the component that generates the event. Some common AWT components that can generate
events include:
• Button: Generates an ActionEvent when clicked.
• TextField: Generates ActionEvent when Enter is pressed.
• Label: Does not generate events by itself, but can listen to other events like mouse clicks.
• Checkbox: Generates ItemEvent when checked or unchecked.
• Mouse: Generates MouseEvent when the mouse is clicked, pressed, or released.
• Keyboard: Generates KeyEvent when keys are pressed or released.
4. Event Listener Interfaces
Java provides various listener interfaces, and the key ones are as follows:
• ActionListener: Handles action events, such as button clicks or menu selections.
public void actionPerformed(ActionEvent e);
• MouseListener: Handles mouse events (click, press, release).
public void mouseClicked(MouseEvent e);
public void mousePressed(MouseEvent e);
public void mouseReleased(MouseEvent e);
public void mouseEntered(MouseEvent e);
public void mouseExited(MouseEvent e);
• KeyListener: Handles keyboard events (key press, key release).
public void keyPressed(KeyEvent e);
public void keyReleased(KeyEvent e);
public void keyTyped(KeyEvent e);
• WindowListener: Handles window events like opening and closing.
public void windowOpened(WindowEvent e);
public void windowClosing(WindowEvent e);
public void windowClosed(WindowEvent e);
public void windowIconified(WindowEvent e);
public void windowDeiconified(WindowEvent e);
public void windowActivated(WindowEvent e);
public void windowDeactivated(WindowEvent e);
public InnerClassExample() {
b = new Button("Click Me");
b.setBounds(100, 100, 80, 30);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
add(b);
setSize(300, 200);
setVisible(true);
}
public AnonymousClassExample() {
b = new Button("Click Me");
b.setBounds(100, 100, 80, 30);
// Anonymous class for event handling
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
add(b);
setSize(300, 200);
setVisible(true);
}
7. AWT Classes
Here are some common AWT components used in Java for building user interfaces:
• Label: Displays a single line of text (static content).
Label label = new Label("Hello, World!");
• Button: Represents a clickable button.
Button button = new Button("Click Me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
});
• TextField: A single-line text input field.
TextField textField = new TextField("Default Text");
• TextArea: A multi-line text input field.
TextArea textArea = new TextArea("Default Text");
• Checkbox: A checkbox for selecting/deselecting options.
Checkbox checkbox = new Checkbox("Accept Terms");
• Choice: A dropdown list of items.
Choice choice = new Choice();
choice.add("Option 1");
choice.add("Option 2");
• List: A list of items that can be selected.
List list = new List();
list.add("Item 1");
list.add("Item 2");
Summary
• Event Handling: The Delegation Event Model uses listeners and event sources to manage user actions.
• Event Listener Interfaces: Interfaces like ActionListener, MouseListener, KeyListener, etc., define methods to handle
various events.
• Adapter Classes: Provide default empty implementations of listener interfaces to simplify event handling.
• Inner/Anonymous Classes: Used for concise event handling implementations.
• AWT Components: Classes like Label, Button, TextField, etc., allow creating user interface components.