0% found this document useful (0 votes)
2 views

EventHandlingChatGpt

The document provides an overview of various event classes and listener interfaces in Java AWT for handling user interactions with GUI components. It includes detailed explanations and example code for ActionEvent, AdjustmentEvent, ComponentEvent, ContainerEvent, ItemEvent, KeyEvent, and MouseEvent. Each section describes the characteristics of the events and demonstrates how to implement the corresponding listener interfaces.

Uploaded by

anudeep g
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

EventHandlingChatGpt

The document provides an overview of various event classes and listener interfaces in Java AWT for handling user interactions with GUI components. It includes detailed explanations and example code for ActionEvent, AdjustmentEvent, ComponentEvent, ContainerEvent, ItemEvent, KeyEvent, and MouseEvent. Each section describes the characteristics of the events and demonstrates how to implement the corresponding listener interfaces.

Uploaded by

anudeep g
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java AWT provides several event classes and corresponding listener interfaces to

handle user interactions with GUI components. Here is an explanation of each event
class and listener interface, along with example code to demonstrate their usage:

1. ActionEvent and ActionListener


Characteristics:
ActionEvent: Generated when a button is pressed, a menu item is selected, or a list
item is double-clicked.
ActionListener: Interface to handle action events.
Example:

java
Copy code
import java.awt.*;
import java.awt.event.*;

public class ActionEventExample {


public static void main(String[] args) {
Frame frame = new Frame("ActionEvent Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());

Button button = new Button("Click Me");


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button was clicked!");
}
});

frame.add(button);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
}
}
In this example, clicking the button generates an ActionEvent which is handled by
the ActionListener.

2. AdjustmentEvent and AdjustmentListener


Characteristics:
AdjustmentEvent: Generated by adjustable components like scrollbars.
AdjustmentListener: Interface to handle adjustment events.
Example:

java
Copy code
import java.awt.*;
import java.awt.event.*;

public class AdjustmentEventExample {


public static void main(String[] args) {
Frame frame = new Frame("AdjustmentEvent Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());
Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 100);
scrollbar.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println("Scrollbar value: " + e.getValue());
}
});

frame.add(scrollbar);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
}
}
In this example, moving the scrollbar generates an AdjustmentEvent which is handled
by the AdjustmentListener.

3. ComponentEvent and ComponentListener


Characteristics:
ComponentEvent: Generated when a component is resized, moved, shown, or hidden.
ComponentListener: Interface to handle component events.
Example:

java
Copy code
import java.awt.*;
import java.awt.event.*;

public class ComponentEventExample {


public static void main(String[] args) {
Frame frame = new Frame("ComponentEvent Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());

frame.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent e) {
System.out.println("Frame resized");
}
public void componentMoved(ComponentEvent e) {
System.out.println("Frame moved");
}
public void componentShown(ComponentEvent e) {
System.out.println("Frame shown");
}
public void componentHidden(ComponentEvent e) {
System.out.println("Frame hidden");
}
});

frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
}
}
In this example, resizing, moving, showing, or hiding the frame generates
ComponentEvents which are handled by the ComponentListener.

4. ContainerEvent and ContainerListener


Characteristics:
ContainerEvent: Generated when a component is added to or removed from a container.
ContainerListener: Interface to handle container events.
Example:

java
Copy code
import java.awt.*;
import java.awt.event.*;

public class ContainerEventExample {


public static void main(String[] args) {
Frame frame = new Frame("ContainerEvent Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());

Panel panel = new Panel();


frame.add(panel);

frame.addContainerListener(new ContainerListener() {
public void componentAdded(ContainerEvent e) {
System.out.println("Component added: " + e.getChild());
}
public void componentRemoved(ContainerEvent e) {
System.out.println("Component removed: " + e.getChild());
}
});

Button button = new Button("Add Button");


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Button newButton = new Button("New Button");
panel.add(newButton);
panel.validate();
frame.validate();
}
});

frame.add(button);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
}
}
In this example, adding a new button to the panel generates a ContainerEvent which
is handled by the ContainerListener.

5. ItemEvent and ItemListener


Characteristics:
ItemEvent: Generated by components that have selectable items (e.g., checkboxes,
choice, list).
ItemListener: Interface to handle item events.
Example:

java
Copy code
import java.awt.*;
import java.awt.event.*;

public class ItemEventExample {


public static void main(String[] args) {
Frame frame = new Frame("ItemEvent Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());

Checkbox checkbox = new Checkbox("Check me");


checkbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("Checkbox checked");
} else {
System.out.println("Checkbox unchecked");
}
}
});

frame.add(checkbox);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
}
}
In this example, checking or unchecking the checkbox generates an ItemEvent which
is handled by the ItemListener.

6. KeyEvent and KeyListener


Characteristics:
KeyEvent: Generated when a key is pressed, released, or typed.
KeyListener: Interface to handle key events.
Example:

java
Copy code
import java.awt.*;
import java.awt.event.*;

public class KeyEventExample {


public static void main(String[] args) {
Frame frame = new Frame("KeyEvent Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());

TextField textField = new TextField(20);


textField.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
System.out.println("Key released: " + e.getKeyChar());
}
public void keyTyped(KeyEvent e) {
System.out.println("Key typed: " + e.getKeyChar());
}
});

frame.add(textField);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
}
}
In this example, pressing, releasing, or typing a key in the text field generates
KeyEvents which are handled by the KeyListener.

7. MouseEvent and MouseListener


Characteristics:
MouseEvent: Generated when the mouse is clicked, pressed, released, entered, or
exited.
MouseListener: Interface to handle mouse events.
Example:

java
Copy code
import java.awt.*;
import java.awt.event.*;

public class MouseEventExample {


public static void main(String[] args) {
Frame frame = new Frame("MouseEvent Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());

Panel panel = new Panel();


panel.setSize(200, 200);
panel.setBackground(Color.LIGHT_GRAY);
panel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked at (" + e.getX() + ", " +
A network error occurred. Please check your connection and try again. If this issue
persists please contact us through our help center at help.openai.com.

You might also like