When we click on button in a frame, some action generates, as a result the state of an object change which is known as JAVA event.
In delegation model, an event is an object that describes a state change in a source. Some of the activities that cause events to be generated are clicking a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse.
Events may occur that are not directly caused by interactions with a user interface. For example, an event may be generated when a timer expires, a counter exceeds a value, a software or hardware failure occurs, or an operation is completed.
Events are supported by a number of packages like java.util, java.awt, and java.awt.event. The java.awt.event package provides many event classes and Listener interfaces for event handling.
Event handling has three main components,
Event Classes | Description | Listener Interface |
---|---|---|
ActionEvent | generated when button is pressed, menu-item is selected, list-item is double clicked | ActionListener |
MouseEvent | generated when mouse is dragged, moved,clicked,pressed or released also when the enters or exit a component | MouseListener |
KeyEvent | generated when input is received from keyboard | KeyListener |
ItemEvent | generated when check-box or list item is clicked | ItemListener |
TextEvent | generated when value of textarea or textfield is changed | TextListener |
MouseWheelEvent | generated when mouse wheel is moved | MouseWheelListener |
WindowEvent | generated when window is activated, deactivated, deiconified, iconified, opened or closed | WindowListener |
ComponentEvent | generated when component is hidden, moved, resized or set visible | ComponentEventListener |
ContainerEvent | generated when component is added or removed from container | ContainerListener |
AdjustmentEvent | generated when scroll bar is manipulated | AdjustmentListener |
FocusEvent | generated when component gains or loses keyboard focus | FocusListen |
To perform event handling, we have to follow the following two steps:
import java.awt.*; import java.awt.event.*; class DemoEvent extends Frame implements ActionListener { TextField tf; DemoEvent() { tf=new TextField(); tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(100,120,80,30); b.addActionListener(this); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e) { tf.setText("Welcome 2 SILAN SOFTWARE"); } public static void main(String[] args) { new DemoEvent(); } }
import java.awt.*; import java.awt.event.*; class DemoEvent2 extends Frame { TextField tf; DemoEvent2() { tf=new TextField(); tf.setBounds(60,50,170,20); Button b=new Button("OK"); b.setBounds(100,120,80,30); Outer ob=new Outer(this); b.addActionListener(ob); //passing outer class instance add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public static void main(String[] args) { new DemoEvent2(); } } class Outer implements ActionListener { DemoEvent2 obj; Outer(DemoEvent2 obj) { this.obj=obj; } public void actionPerformed(ActionEvent e) { obj.tf.setText("SILAN SOFTWARE"); } }
import java.awt.*; import java.awt.event.*; class DemoEvent3 extends Frame { TextField tf; DemoEvent3() { tf=new TextField(); tf.setBounds(60,50,170,20); Button b=new Button("OK"); b.setBounds(50,120,80,30); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tf.setText("SILAN SOFTWARE"); } }); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public static void main(String[] args) { new DemoEvent3(); } }
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc