Monthly Archives: September 2012

Using Action Listeners in Java (continued with previous)

As we discussed in previous post, this is continuing back again for some extend. This simple demonstration is about adding an action to the event performed by the user in application.

In below application it contain a JFrame window, flow layout for the content pane and 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
import java.awt.*;
import javax.swing.*;
 
public class ButtonListener extends JFrame
{
 
public ButtonListener(String title)
{
super (title);
setBounds(100,100,200,150);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setResizable(false);
 
Container c = getContentPane();
c.setLayout (new FlowLayout());
 
JButton b=new JButton("Click Me");
c.add(b);
}
 
public static void main (String[]args)
{JFrame f = new ButtonListener("Button Listner");
f.show();
}
}
import java.awt.*;
import javax.swing.*;

public class ButtonListener extends JFrame
{

public ButtonListener(String title)
{
super (title);
setBounds(100,100,200,150);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setResizable(false);

Container c = getContentPane();
c.setLayout (new FlowLayout());

JButton b=new JButton("Click Me");
c.add(b);
}

public static void main (String[]args)
{JFrame f = new ButtonListener("Button Listner");
f.show();
}
}

This is how it looks like when it runs.

Button Listener Window

A component can be activated by adding “Action Listeners” to them. Now I want to display a message box with the message “You clicked on JButton” when user clicks on the button.

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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class ButtonListener extends JFrame implements ActionListener
{
 
public ButtonListener(String title)
{
super (title);
setBounds(100,100,200,150);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setResizable(false);
 
Container c = getContentPane();
c.setLayout (new FlowLayout());
 
JButton b=new JButton("Click Me");
b.addActionListener(this);
c.add(b);
}
 
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"You clicked on JButton");
}
 
public static void main (String[]args)
{JFrame f = new ButtonListener("Button Listner");
f.show();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonListener extends JFrame implements ActionListener
{

public ButtonListener(String title)
{
super (title);
setBounds(100,100,200,150);
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setResizable(false);

Container c = getContentPane();
c.setLayout (new FlowLayout());

JButton b=new JButton("Click Me");
b.addActionListener(this);
c.add(b);
}

public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"You clicked on JButton");
}

public static void main (String[]args)
{JFrame f = new ButtonListener("Button Listner");
f.show();
}
}

public class ButtonListener extends JFrame implements ActionListener

First of all we implement our class as an action listener by adding “implements ActionListener” to the end of the class title. Now we have implemented required sub classes in our main public class “ButtonListener”.

import java.awt.event.*;

We need to import this package which contain events before use them in our class.

public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,”You clicked on JButton”);
}

This is the method for our event. This method invokes when ever user perform any action on the form. JOptionPane.showMessageDialog(null,”You clicked on JButton”); is the statement for display the message box in our application. JOptionPane is a class which produces message boxes, input boxes, dialog boxes and etc.

b.addActionListener(this);

We add the action listener to the button component. In here we have implemented all necessary sub classes for the events in our public class. Therefore we use the keyword “this” instead of the class name.

See what after compiling and running the application.

Message Box

When user click on the “b” button it executes the “actionPerformed” event. Then immediately it comes across the public class which works as the action listener. Then ButtonListener class invokes the method actionPerformed() in it. The command on the actionPerformed() method is JOptionPane.showMessageDialog(null,”You clicked on JButton”); showing the messagebox to the user.