Results 1 to 3 of 3
  1. #1
    Baldie is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default How do you access a defined object from another class?!?!

    It's a freaking' simple and basic question, but I'm pulling my hair out cause of this. I am creating a GUI for another program. Now, I'm placing each JMenuItem in its own class, so it's easier to attach stuff to each menu item later on. All the JMenuItems are later linked to a JMenu. The problem comes when I cannot attach the JMenuItem to the JMenu. Both the files are in the same directory. Here's the codes.( And oh yeah, with the two codes below, newMenuItem compiles, but fileMenu item doesn't compile and gives this error:<cannot find symbol : variable newMenuItem>) Can anyone tell me how to attach JMenuItem to JMenu? (There's supposed to be more than one JMenuItem to the JMenu, but I simplified it to one JMenuItem first)

    Java Code:
    import javax.swing.JMenu;
    
    public class fileMenu {
    	public void fiM() {
    		JMenu fileMenu = new JMenu("File");
    
    		fileMenu.add(newMenuItem);	
    	}
    }
    Java Code:
    import javax.swing.JMenu;
    
    public class newMenuItem {
    	public void neMI() {
    		JMenuItem newMenuItem = new JMenuItem("New");
    	}
    }
    Thanks in advance.

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

    Default

    Your newMenuItem class should extend MenuItem (though there are valid arguments to not do this, but I won't go into this here). Then if you want to create a new object of it, you'll need to use the new operator just as you would for creating an object of any class.

  3. #3
    Baldie is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Oh cool, it works. This problem's been killing me for at least a week, and now I find out that I've been doing it the wrong way........ let me find a knife and commit seppuku...... thanks a bunch Fubarable.

Similar Threads

  1. Problem with user defined class in ArrayList
    By anders73 in forum New To Java
    Replies: 4
    Last Post: 04-26-2011, 03:59 PM
  2. Replies: 6
    Last Post: 01-26-2011, 07:49 PM
  3. Accessing class defined within a constructor
    By kwaspl in forum New To Java
    Replies: 4
    Last Post: 12-21-2009, 02:35 PM
  4. Creating a Defined Class with Member Functions
    By New2Java in forum New To Java
    Replies: 6
    Last Post: 08-05-2009, 09:05 PM
  5. How to call defined java class in Jsp
    By asheeshiit in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-04-2009, 04:21 AM

Posting Permissions

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