Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default ActionListener for Menu Items

    Hey there, I'm having a few issues with this line of code:
    Java Code:
    e = new JMenuItem("Exit");
    e.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent ae) {
    		// Exit the program
    		System.exit(0);
    	}
    });
    	
    add(e);
    For some reason, NetBeans is telling me I'm having trouble with the 2nd and 3rd lines of this code. It seems fine to me though.

    Thanks for any help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    The compiler is telling me this:
    Java Code:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: javax.swing.JMenuItem.addActionListener
    	at fileMenu.<init>(fileMenu.java:28)
    	at menuBar.<init>(menuBar.java:11)
    	at mainClass.main(mainClass.java:14)
    Java Result: 1
    Line 28 is that "e.addActionListener(..."

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The code looks fine, but lets see what the errors are when you attempt to run it?

    Is the code long? If it isn't please post it so I can see how everything is done.

  5. #5
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    The code looks fine, but lets see what the errors are when you attempt to run it?

    Is the code long? If it isn't please post it so I can see how everything is done.
    Well, here is the whole "fileMenu.java" file:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class fileMenu extends JMenu {
    	public static JMenuItem n;			// New
    	public static JMenuItem o;			// Open
    	public static JMenuItem s;			// Save
    	public static JMenuItem sa;			// Save As
    	public static JMenuItem e;			// Exit
    	
    	public fileMenu() {
    		super("File");
    		n = new JMenuItem("New");
    		add(n);
    		
    		o = new JMenuItem("Open");
    		add(o);
    		addSeparator();
    		
    		s = new JMenuItem("Save");
    		add(s);
    		
    		sa = new JMenuItem("Save As");
    		add(sa);
    		addSeparator();
    		
    		e = new JMenuItem("Exit");
    		e.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				// Exit the program
    				System.exit(0);
    			}
    		});
    		
    		add(e);
    	}
    }
    Not sure if it would help much.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  7. #7
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Do you import ActionListener and ActionEvent?

    By the way, none of those variables should be static.
    Wow, I feel like an idiot, thanks.
    (Thanks for the static catch too.)
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I know this is a nitpick, however; I suggest you not get into the habit of abbreviating so much. It's not many more characters to type open, save, newItem, etc, and it makes your code clearer(I know the comment shows you what they mean) but it's easier to understand the meaning later when you use the variables.

    If you are satisfied, please mark your thread solved with the thread tools at the top.

Similar Threads

  1. Struts menu items delay
    By seol_sensei in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 05-31-2011, 01:20 AM
  2. folder/file menu items
    By jhughes in forum Advanced Java
    Replies: 2
    Last Post: 07-21-2009, 10:57 PM
  3. How to remove Eclipse context menu items?
    By Neil_ in forum Eclipse
    Replies: 0
    Last Post: 04-06-2009, 05:09 AM
  4. Replies: 7
    Last Post: 05-09-2008, 07:54 AM
  5. hyperlinks from menu items?
    By asterope118 in forum New To Java
    Replies: 1
    Last Post: 07-16-2007, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •