Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2005

Tip: Looking for answers? Try searching our database.

Easy Adapter class question

Thread view: 
tomk4567@nowhere.com - 13 Aug 2005 04:20 GMT
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);
    }   
}
tomk4567@nowhere.net - 13 Aug 2005 04:39 GMT
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);
         }    
      }       
    }
Roedy Green - 13 Aug 2005 04:51 GMT
On Sat, 13 Aug 2005 04:16:07 GMT, tomk4567@nowhere.com wrote or quoted

>//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.
tomk4567@nowhere.net - 13 Aug 2005 05:10 GMT
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.
Andrew Thompson - 13 Aug 2005 08:41 GMT
> On Sat, 13 Aug 2005 04:16:07 GMT, tomk4567@nowhere.com 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. )

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
A By-Product Of The TV Industry

Roedy Green - 14 Aug 2005 22:28 GMT
>( 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.
Andrew Thompson - 15 Aug 2005 02:56 GMT
>>( 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??

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

Signature

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)'

Monique Y. Mudama - 16 Aug 2005 00:40 GMT
> 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.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.