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 / January 2006

Tip: Looking for answers? Try searching our database.

How to implement ActionListener

Thread view: 
elingtse@gmail.com - 08 Jan 2006 23:58 GMT
Hi all,

I have problem to call the ActionListener but have no idea what is
going wrong,  Please help. Thankyou.

When compile, the error message show :
-----------------------------------------------------------
C:\>javac TestEditorFrame.java

.\EditorFrame.java:7: cannot resolve symbol
symbol  : class ActionListener
location: class EditorFrame
public class EditorFrame extends JFrame implements ActionListener{

      ^
.\EditorFrame.java:45: cannot resolve symbol
symbol  : class ActionEvent
location: class EditorFrame
       public void actionPerformed(ActionEvent ae){
                                                ^
.\EditorFrame.java:40: addActionListener(java.awt.event.ActionListener)
in javax
.swing.AbstractButton cannot be applied to (javax.swing.JMenuItem)
       itemSave.addActionListener(itemSave);
                    ^
3 errors

Class EditorFrame
---------------------------
import java.awt.*;
import javax.swing.*;

public class EditorFrame extends JFrame implements ActionListener{
    //Attribute
    private JTextArea textArea;
    private JMenuBar menubar = new JMenuBar();
    private JMenu fileMenu = new JMenu("File");
    private JMenuItem itemSave = new JMenuItem("Save");

    //constructor
    public EditorFrame(){
        Container contentPane = getContentPane();
        String content = ("This is a text.\nSecond Line.");
        textArea = new JTextArea(content, 3, 20);
        setJMenuBar(menubar);
        menubar.add(fileMenu);
        fileMenu.add(itemSave);
                               contentPane.add(textArea);
                               itemSave.addActionListener(this);
    }

    //Action Listener Method
    public void actionPerformed(ActionEvent ae){
        System.out.println("ok");
    }
}

Class TestEditorFrame
---------------------------------
import java.awt.*;
import javax.swing.*;

//Definition of class TestEditorFrame
public class TestEditorFrame extends JFrame {

    //Main executive method
    public static void main(String args[]){

        //create an EditorFrame object
        EditorFrame anEditor = new EditorFrame();

        anEditor.setVisible(true);

        //set when the close button is clicked, the application exits
        anEditor.setDefaultCloseOperation(EXIT_ON_CLOSE);
       
        //Set the GUI to its optimal size
        anEditor.pack();
       
    }
}
zero - 09 Jan 2006 13:11 GMT
elingtse@gmail.com wrote in news:1136648086.250036.316200
@g47g2000cwa.googlegroups.com:

> Hi all,
>
[quoted text clipped - 22 lines]
>                      ^
> 3 errors

<code snipped>

Rather than just hand you the solution, I'll tell you where you can find
it.

Look at:

http://java.sun.com/j2se/1.5.0/docs/api/

Bookmark this page, you'll need it a lot.

In the left bottom frame, look for "ActionListener" and "ActionEvent",
the two classes that are mentioned in your error messages.  Check both of
their pages.  At the top of the page (directly above the title), you'll
see the packages that those classes belong to.

Make sure you import all the classes you use in the program!

Hope this helps,
Zero

Signature

Beware the False Authority Syndrome

ricky.clarkson@gmail.com - 13 Jan 2006 02:10 GMT
I would strongly recommend importing each class you use, individually,
rather than using the .* syntax.

import javax.swing.JButton;
import javax.swing.JFrame;

etc.

Now look for the import that imports ActionListener in your code.  Oh,
it's not there?!


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.