Events in Java
Previous
|<Graphical User Interface (GUI) |
Next
Drawing in the window>| |
In order for the graphical user interface (GUI) to have some functionality, i.e. to enable user interaction with the GUI, java events need to be created. Working with programming events implies that when an event occurs in an application, e.g. clicking on a particular swing package component or a specific key on the keyboard invokes a particular method in response to an event.
An event listener is required to respond to an event. an object whose appropriate method will be called to execute in response to that event.
In working with events, interfaces from the public class library are used to give functionality to objects that listen to events. Those are:
An event listener is an object that implements one of the interfaces to give functionality to events.
An event listener is required to respond to an event. an object whose appropriate method will be called to execute in response to that event.
In working with events, interfaces from the public class library are used to give functionality to objects that listen to events. Those are:
- ActionListener - used when clicking on a specific component (eg button)
- ItemListener - used when clicking on a ComboBox drop-down list item
- KeyListener - Clicking on a specific key on the keyboard
An event listener is an object that implements one of the interfaces to give functionality to events.
Example: Button Click Event:
The click of a button produces an ActionEvent class object and this object carries information about that object. An "event listener" is required to respond to the click of a button. an object of some class that implements the ActionListener interface. The method that will be called as an event response is defined in the interface and is called actionPerformed and gets an object of the ActionEvent class as an argument to that method.
To show this we will create one button and one label with the text as shown:
To show this we will create one button and one label with the text as shown:
Let's look at the following code within the main method:
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
* @param args the command line arguments
*/
public static void main(String[] args) {
//Creating object
JFrame win = new JFrame("Button Events");
win.setSize(500, 300);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.getContentPane().setLayout(new FlowLayout());
//Text label
JLabel label = new JLabel("No buttons were clicked");
label.setSize(150, 25);
win.getContentPane().add(label);
//Button
JButton dugme = new JButton("Apply");
dugme.setSize(100, 25);
win.getContentPane().add(dugme);
/*
* visibility is set, the JFrame object
* is invisible by default
*/
win.setVisible(true);//the last command will remain
}JFrame win = new JFrame("Button Events");
win.setSize(500, 300);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.getContentPane().setLayout(new FlowLayout());
//Text label
JLabel label = new JLabel("No buttons were clicked");
label.setSize(150, 25);
win.getContentPane().add(label);
//Button
JButton dugme = new JButton("Apply");
dugme.setSize(100, 25);
win.getContentPane().add(dugme);
/*
* visibility is set, the JFrame object
* is invisible by default
*/
win.setVisible(true);//the last command will remain
Figure 2: Graphics in Java. Create frames and labels and button-code
To add a button click event, we first need to add an event listener. It is a class object that implements the ActionListener interface. Since this is an interface from the public class library. it just needs to be linked to the current class using the official word implements.
We see that an error is displayed. This is because we are obliged to complete the abstract methods from the ActionListener interface in the ClickButtonEvent class because we have linked it to this class using the word interface. Clicking on the light on the left will netbeans enter a method that is abstract in the interface.
In fact, only one actionPerformed method needs to be implemented in this interface, which will be called in response to a button click event in java.
win.setVisible(true);//the last command will remain
}@Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException( "Not supported yet."); //To change body of generated methods...
}Creating button events. Overriding the actionPerformed method
You should delete the content that was inserted automatically after clicking on the light and write your own code, the one that executes after the click of a button.
Now we're building an event-listening facility:
Now we're building an event-listening facility:
package solarsystem;
import import javax.swing.*; //Import of class JFrame
//ActionListener
public class ClickButtonEvent implements ActionListener{
static ClickButtonEvent listener = new ClickButtonEvent();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
import import javax.swing.*; //Import of class JFrame
//ActionListener
public class ClickButtonEvent implements ActionListener{
static ClickButtonEvent listener = new ClickButtonEvent();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Figure 6: Events in Java. Creating an Event Listener
We must also indicate the component being listened to by adding a listener to that component. This component is a button, and we add the listener as shown:
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Button
JButton button = new JButton("Apply");
button .setSize(100, 25);
win.getContentPane().add(button );
dugme.addActionListener(listener);
/*
* visibility is set, the JFrame object
* is invisible by default
*/
win.setVisible(true);//the last command will remain
}
* @param args the command line arguments
*/
public static void main(String[] args) {
//Button
JButton button = new JButton("Apply");
button .setSize(100, 25);
win.getContentPane().add(button );
dugme.addActionListener(listener);
/*
* visibility is set, the JFrame object
* is invisible by default
*/
win.setVisible(true);//the last command will remain
Figure 7: Events in Java. Adding event listeners
Now the actionPerformed method should be called in response to a button click.
We will check that this method is indeed called if, for example, we write code that will print a message on the label. The text on the label is printed using the setText method:
We will check that this method is indeed called if, for example, we write code that will print a message on the label. The text on the label is printed using the setText method:
The figure shows that when after the object reference we put the operator "." the list of attributes and methods of the label object does not open. Because this label was created in the main method as local in the second method, it is not available. To be available, it must be declared as a class attribute:
package clickbuttonevent;
import import javax.swing.*; //Import of all class in package swing
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
//ActionListener
public class ClickButtonEvent implements ActionListener{
static ClickButtonEvent listener = new ClickButtonEvent();
static JLabel label;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
import import javax.swing.*; //Import of all class in package swing
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
//ActionListener
public class ClickButtonEvent implements ActionListener{
static ClickButtonEvent listener = new ClickButtonEvent();
static JLabel label;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Creating object
JFrame win = new JFrame("Button Events");
win.setSize(500, 300);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.getContentPane().setLayout(new FlowLayout());
//Text label
label = new JLabel("No buttons were clicked");
label.setSize(150, 25);
win.getContentPane().add(label);
JFrame win = new JFrame("Button Events");
win.setSize(500, 300);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.getContentPane().setLayout(new FlowLayout());
//Text label
label = new JLabel("No buttons were clicked");
label.setSize(150, 25);
win.getContentPane().add(label);
Figure 8: Events in Java. Creating an Event Click Information Label
The declaration has now been moved beyond the main method but still within the class to represent an attribute for the class. Now we're trying to access the methods of that object again in the actionPerformed method:
No more mistakes now. Let's start the application and after the click of the button the text in the label changes to "Clicked button", which shows that the actionPerformed method is really called when the button click event occurs.
Previous
|<Graphical User Interface (GUI) |
Next
Drawing in the window>| |