An event is an action that takes place within a program, such as the clicking of a button. Event is a change in the state of an object. That is, the event describes the change in the state of the source. Events are generated as a result of user interaction with the graphical user interface components. Examples include moving the mouse, clicking a mouse button etc.
When an event takes place, the component that is responsible for the event creates an event object in memory. The event object contains information about the event. The component that generated the event object is called the event source.
For example, when a user clicks a button, the JButton component generates an event object. The JButton component that generated the event object is called the event source
If the source component, that is the JButton, is connected to an event listener, the event object is automatically passed as an argument to a specific method in the event listener. The method then performs any actions that it was programmed to perform in response to the event. This process is referred to as event firing.
When you write a GUI application, you have to write event listeners for all the possible events that can be created by the components in the application.
Program to demonstrate event handling of a JButton
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package villagecoder; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /* * This program demonstrates event handling of action event from a JButton */ public class JButtonEventHandling extends JFrame { private final int WINDOW_WIDTH=300; //width of the frame private final int WINDOW_HEIGHT=150; //height of the frame private JPanel panel; //panel to hold components private JButton button; //a button public JButtonEventHandling(){ //set the title of the frame setTitle("JButton Event Handling"); //specify an action for the close operation setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the size of the frame setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //build panel buildPanel(); //add the panel to the frame add(panel); //show frame setVisible(true); } /* * This method creates a panel and add the button component */ public void buildPanel(){ //create a panel panel=new JPanel(); //create a button button=new JButton("change color"); //register the button with an action listener button.addActionListener(new ButtonListener()); //add button to the panel panel.add(button); } /* * Private inner class to respond action event from JButton */ private class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ panel.setBackground(Color.red); } } /* * The main method */ public static void main(String[] args) { //create an instance of the class new JButtonEventHandling(); } } |
Output
After clicking on the button, an action event is generated. This causes the private inner class, ButtonListener to run setting the content pane of the JFrame object to red.
Recent Comments