Easy Adapter class question

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.
 
T

tomk4567

Basic question regarding using a seperate adapter class:

Code doesnt compile. What argument do I use with the eventAdapter class to
make the parent class ie the MenuDialog2 class that extneds JFrame bring up
the appropriate OptionPane.

'this' doesnt compile b/c the EvenAdapter class cant bring up the
JOptionPane.

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

public class MenuDialog2 extends JFrame {
JMenuItem message = new JMenuItem("message");
JMenuItem plain = new JMenuItem("plain");
JMenuItem error = new JMenuItem("error");
JMenuItem info = new JMenuItem("info");
JMenuItem question = new JMenuItem("question");

JOptionPane jp = new JOptionPane(); //global component

//seperate adapter class for events
EventAdapter adapter = new EventAdapter();

Color color = Color.black;
DrawOn canvas = new DrawOn();

public MenuDialog2(String title){
super(title);
Container c = getContentPane();
c.add(canvas);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);

JMenu dialogMenu = new JMenu("Dialog");
bar.add(dialogMenu);
dialogMenu.add(message);
dialogMenu.addSeparator();

dialogMenu.add(plain); dialogMenu.add(error); dialogMenu.add(info);
dialogMenu.add(question); dialogMenu.add(warning);

//add event listeners
message.addActionListener(adapter);
plain.addActionListener(adapter);
error.addActionListener(adapter);
info.addActionListener(adapter);
question.addActionListener(adapter);
}

//application panel
class DrawOn extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Color oldColor = g.getColor();
g.setColor(color);
g.setFont(new Font("Serif", Font.BOLD, 24));
g.drawString("Have a good day", 50,100);
g.setColor(oldColor);
}
}

//seperate adapter class
class EventAdapter implements ActionListener {
//must implement actionPerformed
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("message")) {

//compiler error here:

jp.showMessageDialog( ??????, "Your message goes here" );
} else if (command.equals("plain")) {
jp.showMessageDialog(????, "Very Plain Message" , "Plain Msg",
JOptionPane.PLAIN_MESSAGE);
} else if (command.equals("error")) {
jp.showMessageDialog(this, "Made an Error", "Erro Msg",
JOptionPane.ERROR_MESSAGE);
} else if (command.equals("info")) {
jp.showMessageDialog(this, "Information", "Some
info",JOptionPane.INFORMATION_MESSAGE);
} else if (command.equals("question")) {
jp.showMessageDialog(this, "Question" ,"????" ,
JOptionPane.QUESTION_MESSAGE);
}
}
}

public static void main(String[] args) {
MenuDialog2 f = new MenuDialog2("Dialog Test");
f.setSize(300, 200); f.setVisible(true);
}
}
 
T

tomk4567

Nevermind!

//seperate adapter class
class EventAdapter implements ActionListener {
//must implement actionPerformed
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("message")) {
jp.showMessageDialog(MenuDialog2.this, "Your message goes here" );
} else if (command.equals("plain")) {
jp.showMessageDialog(MenuDialog2.this, "Very Plain Message" , "Plain
Msg", JOptionPane.PLAIN_MESSAGE);
} else if (command.equals("error")) {
jp.showMessageDialog(MenuDialog2.this, "Made an Error", "Erro Msg",
JOptionPane.ERROR_MESSAGE);
} else if (command.equals("info")) {
jp.showMessageDialog(MenuDialog2.this, "Information", "Some
info",JOptionPane.INFORMATION_MESSAGE);
} else if (command.equals("question")) {
jp.showMessageDialog(MenuDialog2.this, "????" ,"????" ,
JOptionPane.QUESTION_MESSAGE);
} else if (command.equals("warning")) {
jp.showMessageDialog(MenuDialog2.this, "This is a warning", "Uh-oh!",
JOptionPane.WARNING_MESSAGE);
}
}
}
 
T

tomk4567

Thx for the help! I saw the mistake as soon as I posted the msg.

Thx for the other comments as well!

I will type out 'separate' 100 times.
 
A

Andrew Thompson

On Sat, 13 Aug 2005 04:16:07 GMT, (e-mail address removed) wrote or quoted ....
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.

grep may not have a spell checker, but most search engines do.
<http://www.google.com/search?hl=en&q=seperate>
top of page - "Did you mean: separate ..."

( Of course, spelling the word correctly is better than relying
on a machine to guess what you meant, but they sure try. )
 
R

Roedy Green

( Of course, spelling the word correctly is better than relying
on a machine to guess what you meant, but they sure try. )

But will it find the text in web pages where people have misspelled??

So much stuff lies hidden on the net because people blew the spelling
in their web-pages.

Given how Java won't let you make typos, you'd think Java programmers
would tend to become excellent spellers and typists.

It is a major pain searching somebody else's code who cannot spell.

I tend to spend an unusual amount of time renaming things to use
consistent phrases and spelling. I want to be able to generate names
out of my head, not having to search for them. That requires a
consistent way to come up with them.
 
A

Andrew Thompson

But will it find the text in web pages where people have misspelled??

I suspect your questions can be answered by trying it
and looking at the resulting output.

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
You Can't Prove It Won't Happen
"I knew it was you who did it with your voodoo.." Divinyls 'Siren (Never
Let You Go)'
 
M

Monique Y. Mudama

Given how Java won't let you make typos, you'd think Java
programmers would tend to become excellent spellers and typists.

Nah; java enforces consistency, not accuracy. I can't tell you how
many times I've had to work with legacy code in which the class names
were misspelled. We were using CVS, where renaming is a pain, so I
had to bite my tongue and suffer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top