R
Roedy Green
//seperate adapter class
class EventAdapter implements ActionListener {
//must implement actionPerformed
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("message")) {
Let me get a couple of things off my chest before I attempt to answer
your question.
1. The word is "separate" not "seperate". I see that word spelled
incorrectly more often than correctly. It is beyond a typo; it is
revolutionary statement against standard spelling. Standard spelling
is more important than ever with google and grep.
2. An adapter is a class that provides default implementations of
methods you are not interested in, such as MouseAdapter. If there is
only one method, e.g. in ActionListener then you don't need an
adapter, just a Listener.
You can code that with an anonymous class like this:
ActionListener actionListener = new ActionListener() {
public void actionPerformed( ActionEvent event )
{
...
}
};
or if you are going use it only once drop that entire thing less the
ActionListener actionListener inside this.addActionListener( ... );
3. You ask us to diagnose an error message without telling us what is
was or providing us the code you used to generate it. That code you
posted in not the code you tried to compile.
4. don't put more than one statement per line. see
http://mindprod.com/jgloss/codingconventions.html
5. dialogMenu.add(warning); fails because you did not define warning.
Now to get to the nub of what I think is your question:
Why does the following statement fail to compile?
jp.showMessageDialog( this, "Your message goes here" );
where you have put 'this', showMessageDialog wants a parent Component,
e.g. the Applet or Frame where your dialog is supposed to pop up.
You are feeding it your ActionListener which is not even a Component
much less the parent.
You need to pass it the address of the MenuDialog2 Frame. To get
that you would code:
jp.showMessageDialog( MenuDialog2.this, "Your message goes here" );
to get the this of the outer class.