4

I'm being asked to transform an anonymous button class into an inner button class. The text I'm given to read discusses this topic using examples incorporating an ActonListener. However, the code I'm being asked to modify does not have an ActonListener in it. So I'm having difficulty following what I'm supposed to do. How do I take the following code and convert an anonymous to an inner class. does my button code even have an anonymous class in it?

Caveat: Don't just type up an answer for me. I need to learn this. Please help point me in the right direction.

Here's my code:

package ui.panels;

import java.awt.Panel;

import interfaces.Resettable;
import model.Model;
import ui.panels.ButtonPanel;

public class ControlsPanel extends Panel implements Resettable{  
    private ButtonPanel btnPanel;

    public ControlsPanel (Model model) {
        btnPanel = new ButtonPanel(model);
        add(btnPanel);
    }

    public void resetComponents() {
    }
}

And here is "ButtonPanel.java"

package ui.panels;

import java.awt.Button;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import model.Model;

public class ButtonPanel extends Panel {
  private Button btnClear;

    public ButtonPanel(final Model model) {
        btnClear = new Button("Clear");
        btnClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                model.resetComponents();
                model.repaint();
            }
        });
        add(btnClear);
    }
}
6
  • 1
    What is ButtonPanel? It appears to be non-standard, which will make it tricky to lend advice. (Presumably it contains the actual buttons, but without seeing what it looks like, not sure what you should do.) Furthermore, it does not appear that you are using any inner classes, anonymous or otherwise, in the code you provided. Commented Feb 17, 2011 at 0:41
  • ask your teacher where is the anonymous class Commented Feb 17, 2011 at 0:46
  • 1
    AH HA! I think the anonymous class is actually inside "ButtonPanel.java" argh! I've spent hours looking at the wrong file! Commented Feb 17, 2011 at 0:49
  • Am I correct? If I wait for teacher to respond it will be two days from now. Commented Feb 17, 2011 at 0:50
  • Yeah, it's the new ActionListener() { ... }. I think you've just solved this yourself, so you might as well post your own answer below - thanks. Commented Feb 17, 2011 at 0:50

6 Answers 6

4

The button is not anonymous, but the action listener is. Specifically:

btnClear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        model.resetComponents();
        model.repaint();
    }
});

Here you are invoking the method, addActionListener, and passing as the argument, an implementation of ActionListener. The key here is that this implementation is an anonymous class that implements the interface inline.

Remember, ActionListener is an interface, and interfaces cannot contain actual implementation. Instead, what is happening, is a new class is being declared (without a name) that implements ActionListener. That interface defines one method, actionPerformed, which is the actual handler for the button -- it calls resetComponents and repaint. It is possible to turn this into a new normal class that actually has a name, continues to implement ActionListener, by implementing actionPerformed.

Sign up to request clarification or add additional context in comments.

1 Comment

... and the final challenge is once this anonymous class is promoted to an inner class how is it going to know about model
1

AH HA! I think the anonymous class is actually inside "ButtonPanel.java" argh! I've spent hours looking at the wrong file!

Comments

0

Indeed, there's no anonymous class there. One can imagine transformations running the other way, e.g. new Resettable()....

Comments

0

Well ButtonPanel is non-standard as far as I see so every answer will be hard without further information, but I can give some default answers ;-)

"does my button code even have an anonymous class in it?" No it doesn't. A anonymous class is often created if you want to a instance of a class that implements a specific interface.

A non GUI example would be Comparator. You want to compare an array after a specific criteria - you could create a extra class for this, but you'll use it only once anyhow and there's no real reuseability to speak of. So we'll just create an anonymous class that implements the Comparator interface:

    Comparator<Integer> c = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 - o2;
        }
    };

Nothing special about that - note that your anonymous class can access outside variables, but they're copied when creating the object. To avoid confusion this means that local variables have to be final.

Comments

0

--- edited as question was updated ---

btnClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                model.resetComponents();
                model.repaint();
            }
        });

contains an anonymous class extended off of ActionListener

You need that code to read something like

btnClear.addActionListener(new MyInnerClass());

If you do that with everything working 100% the same, I'd say you did it!

--- original post follows --- The question is worded in such a way that I might understand it; but, I'm not sure.

One hint is that it asked for an inner class. That means that the solution expects you to write an inner class:

public class ControlsPanel extends Panel implements Resettable {

    // must have an inner class
    class SomeButtonModel (or SomeButtonPanel) {
       // code for inner class.
    }

}

Note that if you find an abstract class (or interface) which is instantiated in your code by specifying the missing functions in an initializer block, you've probably found the "anonymous" class. That means your inner class should extend that particular class, and you should rework the problem to use your inner class instead of the anonymous construction.

As usual, the main problem is initially understanding the question asked, once that is done, the rest is just work.

Comments

0

An example of using an anonymous class would be implementing the ActionListener interface "on the fly" so-to-speak without defining a separate class (i.e. a class with a name and therefore not anonymous). For example:

JButton jb = new JButton("click me");
jb.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
          // do stuff
     }
});

The "new ActionListener()" class is anonymous cuz it doesn't have a name.

To create an ActionListener as an inner class inside an outer class (called OuterClass), you would say something like:

public class OuterClass {
    public OuterClass() {
        JButton jb = new JButton("click me");
        jb.addActionListener(new MyActionListener());
    }

    private class MyActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
          // do stuff
        }
    } // end inner class

} // end outer class

Now instead of an anonymous class you have a named inner class, namely "MyActionListener."

Hope that helps

-Ed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.