Wednesday, January 14, 2009

JAVA: Named Inner-class Listeners

Defining an inner class listener to handle events is a very popular style.

  1. Access:-Use an inner class rather than an outer class to access instance variables of the enclosing class. In the example below, the txt1, txt2, txt3 can be referenced by the listener class. Because simple program listeners typically get or set values of other widgets in the interface, it is very convenient to use an inner class.
  2. Reuse:-Unlike anonymous inner class listeners, it's easy to reuse the same listener for more than one control, eg, the click of a button might perform the same action as the equivalent menu item, and might be the same as hitting the enter key in a text field.
  3. Organization:- It's easier to group all the listeners together with inner classes than with anonymous inner class listeners.

EXAMPLE No:1 Using One Inner class for different Events

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/***
*
* @author Sabah u Din Irfan
* @Description ActionListener Example Using Named Inner Classes
*
***/
public class ActionListenerInnerClassDemo extends JFrame{

JLabel lbl ;
JTextField txt1;
JTextField txt2;
JTextField txt3;
JButton btnAdd;
JButton btnSub;

ActionListenerInnerClassDemo()
{
// Set Frame Window properties
super("Action Listner Demo!!");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get Container
Container container = getContentPane();
container.setLayout(new FlowLayout());
// Create Components
lbl = new JLabel(" Please Enter two values in text fields:");
txt1 = new JTextField(15);
txt2 = new JTextField(15);
txt3 = new JTextField(15);
btnAdd = new JButton("Add");
btnSub = new JButton("Sub");
// Add Components

container.add(lbl);
container.add(txt1);
container.add(txt2);
container.add(txt3);
container.add(btnAdd);
container.add(btnSub);

// Add Action Listners.. Use Annonymous Inner Classes
MyListener listener = new MyListener();

btnAdd.addActionListener(listener);
btnSub.addActionListener(listener);

// Make frame visible and set its size
setVisible(true);
pack();
} // end of Constructor

// Private Named Inner Class to Handle the Events
//////////////////////////////////////////////////////
private class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if ( e.getSource() == btnAdd)
{
int a = Integer.parseInt(txt1.getText());
int b = Integer.parseInt(txt2.getText());

int c = a+b;

txt3.setText(c+"");
}
else if( e.getSource() == btnSub)
{
int a = Integer.parseInt(txt1.getText());
int b = Integer.parseInt(txt2.getText());

int c = a-b;

txt3.setText(c+"");
}

}
}// end named inner class MyListener
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ActionListenerInnerClassDemoframe = new ActionListenerInnerClassDemo();

}

}

EXAMPLE No:2 Using Different Inner classes for different Events


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/***
*
* @author Sabah u Din Irfan
* @Description ActionListener Example Using Named Inner Classes
*
***/
public class ActionListenerInnerClassDemo extends JFrame{

JLabel lbl ;
JTextField txt1;
JTextField txt2;
JTextField txt3;
JButton btnAdd;
JButton btnSub;

ActionListenerInnerClassDemo()
{
// Set Frame Window properties
super("Action Listner Demo!!");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get Container
Container container = getContentPane();
container.setLayout(new FlowLayout());
// Create Components
lbl = new JLabel(" Please Enter two values in text fields:");
txt1 = new JTextField(15);
txt2 = new JTextField(15);
txt3 = new JTextField(15);
btnAdd = new JButton("Add");
btnSub = new JButton("Sub");
// Add Components

container.add(lbl);
container.add(txt1);
container.add(txt2);
container.add(txt3);
container.add(btnAdd);
container.add(btnSub);

// Add Action Listners.. Use Annonymous Inner Classes
AddListener addlistener = new AddListener();
SubListener sublistener = new SubListener();

btnAdd.addActionListener(addlistener);
btnSub.addActionListener(sublistener);

// Make frame visible and set its size
setVisible(true);
pack();
} // end of Constructor

// Private Named Inner Classes to Handle the Events
//////////////////////////////////////////////////////
private class AddListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {

int a = Integer.parseInt(txt1.getText());
int b = Integer.parseInt(txt2.getText());

int c = a+b;

txt3.setText(c+"");
}
}// end named inner class
private class SubListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {

int a = Integer.parseInt(txt1.getText());
int b = Integer.parseInt(txt2.getText());

int c = a - b;

txt3.setText(c+"");
}
}// end named inner class MyListener
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ActionListenerInnerClassDemo frame = new ActionListenerInnerClassDemo();

}

}

No comments: