RnMss
RnMss

Reputation: 3843

C++Template in Java?

I want something like this:

public abstract class ListenerEx<LISTENER, PARENT> implements LISTENER {
    PARENT parent;
    public ListenerEx(PARENT p) {
        parent = p;
    }
}

But it doesn't compile. Is there a better solution? Is there something in Java like C++ template that would do check syntax after template deduction?


The following explains why I need such a ListenerEX class, if you already know what it is, you don't need to read the following.

I have a main window, and a button on it, and I want to get access to some method of the main window's within the listener:

public class MainWindow extends JFrame {
    public void doSomething() {
        /* ... */
    }
    public void doSomethingElse() {
        /* ... */
    }

    private JButton button;

    public MainWindow() {
        button = new JButton(...);
        add(button);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doSomething();
                doSomethingElse();
            }
        });
    }
}

This would compile but does not work properly all the time. (Why would it compile when the ActionListener does not have doSomething() method?)

Of course we can do it like this:

public class MainWindow extends JFrame {
    public void doSomething() {
    }
    public void doSomethingElse() {
    }

    private JButton button;

    public MainWindow() {
        button = new JButton(...);
        add(button);

        class ActionListener1 implements ActionListener {
            MainWindow parent;
            public ActionListener(MainWindow p) {
                parent = p;
            }
            public void actionPerformed(ActionEvent e) {
                parent.doSomething();
                parent.doSomethingElse();
            }
        }
        button.addActionListener(new ActionListener1(this));
    }
}

However I hate this style ...

So I tried:

public abstract class ActionListenerEx<P> implements ActionListener {
    P parent;
    public ActionListenerEx(P p) {
        parent = p;
    }
}
public class MainWindow extends JFrame {
    public void doSomething() {
    }
    public void doSomethingElse() {
    }

    private JButton button;

    public MainWindow() {
        button = new JButton(...);
        add(button);
        button.addActionListener(new ActionListenerEx<MainWindow>(this) {
            public void actionPerformed(ActionEvent e) {
                parent.doSomething();
                parent.doSomethingElse();
            }
        });
    }
}

But there's lots of Listeners beside the ActionListener ...

public abstract class ActionListenerEx<LISTENER, PARENT> implements LISTENER {
    PARENT parent;
    public ActionListenerEx(PARENT p) {
        parent = p;
    }
}

However, it won't compile ...

I am fresh at Java, and I wonder if there's already better solution.

Upvotes: 2

Views: 151

Answers (3)

Pradeep Gollakota
Pradeep Gollakota

Reputation: 2181

Blockquote I want something like this:

public abstract class ListenerEx<LISTENER, PARENT> implements LISTENER {
PARENT parent;
public ListenerEx(PARENT p) {
    parent = p;
}

}

But it doesn't compile. Is there a better solution? Is there something in Java like C++ template that would do check syntax after template deduction?

It doesn't compile because u can't parametrize with hardcoded classes/interfaces. So, ListenerEx<LISTENER, PARENT> is meaningless. what you want is

ListenerEx<L extends LISTENER, P extends PARENT>

ugh... so not used to posting on stackoverflow

Upvotes: 1

Pradeep Gollakota
Pradeep Gollakota

Reputation: 2181

Falmarri is correct. Your intial version of code with the Annonymous Inner Class defn of ActionListener is fine. If ur code doesnt work correctly the error is not coming from there.

I notice that u have the ActionListener in the constructor of MainWindow. Make sure that your instance variables are initialized before the doSomething...() methods are called inside the ActionListener.

Another word of extreme caution I'd like to give u is that u r using a generalization relationship between MainWindow and JFrame (parent-child relationship). keep in mind that if u extend JFrame, u inherit all of the methods defined in JFrame. in many cases (esp ones involving gui's) people use a generalization relationship when in fact, they want a composition relationship. It'd be better to have an instance variable that points to a JFrame rather than make it a sub-class of JFrame.

Long story short, check to see if u can write ur code w/o making MainWindow a child of JFrame. if u can, u shouldn't make MainWindow a child of JFrame.

Upvotes: 0

Falmarri
Falmarri

Reputation: 48577

Why would it compile when the ActionListener does not have doSomething() method?)

Because the containing class has the doSomething() method. There's an implicit MainWindow.this prepended there.

What's wrong with your first version? It seems fine. Other than that, what's your question? You're all over the place and posted a bunch of code.

Upvotes: 2

Related Questions