Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Inner-class Listeners

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz


Bookmark and Share

Named inner class

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

  • Access. Use an inner class rather than an outer class to access instance variables of the enclosing class. In the example below, the myGreetingField 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.
  • 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.
  • Organization. It's easier to group all the listeners together with inner classes than with anonymous inner class listeners.

Examples

See Lesson 7 - DogYears - Listeners

Partial source code to share one inner class listener

  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 
// File:  : events/SomePanel.java
// Purpose: Show use of named inner class listener.
// Author : Fred Swartz
// Date   : 2005-09-05

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

class SomePanel extends JPanel {

    private JButton    myGreetingButton = new JButton("Hello");
    private JTextField myGreetingField  = new JTextField(20);

    //=== Constructor
    public SomePanel() {
        ActionListener doGreeting = new GreetingListener();
        myGreetingButton.addActionListener(doGreeting);
        myGreetingField.addActionListener(doGreeting);
        // . . . Layout the panel.
    }


    /////////////////////////// Define inner class as listener.
    private class GreetingListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            myGreetingField.setText("Guten Tag");
        }
    }
}

Getting the name or actionCommand from a button

If you have a listener for a button, you might wonder why you need to get the text that labels the button. A common reason is that you have a number of buttons (eg, the numeric keypad on a calculator) that do almost the same thing. You can create one listener for all the keys, then use the text from the button as the value.

getActionCommand(). By default the getActionCommand() method of an ActionEvent will return the text on a button. However, if you internationalize your buttons so that the show different text depending on the user locale, then you can explicitly set the "actionCommmand" text to something else.

Let's say that you want to put the button value at the end of the inField field as you might want to do for a calculator. You could do something like below (altho checking for overflow).

 JTextField inField = new JTextField(10);
 . . .
// Create an action listener that adds the key name to a field
ActionListener keyIn = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // Get the name of the button
        String keyNum = e.getActionCommand(); // "1", "2", ...
        inField.setText(inField.getText() + keyNum);
    }
};

// Create many buttons with the same listener
JButton key1 = new JButton("1");
key1.addActionListener(keyIn);
JButton key2 = new JButton("2");
key2.addActionListener(keyIn);
JButton key3 = new JButton("3");
key3.addActionListener(keyIn);
 . . .

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy