aspose file tools*
The moose likes Beginning Java and the fly likes Creating one class from another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating one class from another" Watch "Creating one class from another" New topic
Author

Creating one class from another

Craig Boyd
Greenhorn

Joined: Aug 30, 2010
Posts: 21
Hello All,

I have a SIMPLE JFrame class and a SIMPLE JMenuBar class. I want the JFrame class to call/instantiate the JMenuBar that I have written, but I don't think I have the JMenuBar set up right. I get the following two errors when I try to compile it:


Here is the code for MyMenuBar.java:



What am I doing wrong?

Thanks,

Craigbert
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3682
    
  16
Java is case sensitive. Try return


Joanne
Craig Boyd
Greenhorn

Joined: Aug 30, 2010
Posts: 21
Joanne, thanks for the reply. I made the change and now I am getting:

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3682
    
  16
That often suggests your {} are not matching up somewhere. Make sure you haven't got an extra one somewhere or you're not are missing a closing one and that they are all where you intended them to be.

Edit: Or you can just wait for Stephan to tell you where the problem is
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Your return statement is outside your method.
Craig Boyd
Greenhorn

Joined: Aug 30, 2010
Posts: 21
Well, that solved that problem! Thanks!
Now when I try to compile the JFrame I get this error:



Here is the code for MyJFrame:



Am I not calling the MyMenuBar correctly?

Thanks,

Craigbert
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Craig Boyd wrote:Am I not calling the MyMenuBar correctly?

I assume you're trying to create a new one? Then you need a "new".


Edit: although that still won't work, since MyMenuBar isn't a JMenuBar.

So you can either do this:
Or, and I think this is more sensible, have MyMenuBar extend JMenuBar, and swap your main() method for a constructor. For example:
Craig Boyd
Greenhorn

Joined: Aug 30, 2010
Posts: 21
OK...everything compiles and runs, but the menu bar does not appear in the JFrame.

I took the "extends" approach Matthew suggested.

Why would the menu bar not appear?

Thanks,

Craig Boyd
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

luck, db
There are no new questions, but there may be new answers.
Craig Boyd
Greenhorn

Joined: Aug 30, 2010
Posts: 21
Fair enough. I can not seem to find the right file extension to use so I am including the code for both here. They are both VERY small.
They both compile and run w/the exception of the menu bar not showing up.
MyJFrame.java


MyMenuBar.java


Thanks,

Craigbert
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

You haven't actually done what I suggested. You've made MyMenuBar extend JMenuBar, but you haven't made the other changes I said would be necessary:

- change the main() method into a constructor
- add the menus to the MyMenuBar object you're creating, rather than creating another new menu bar and adding them to that.

So the problem at the moment is that two JMenuBars get created. One has all the items added to it, and the other is what you're adding to the JFrame. My last post gave an extract of what needs doing.
Craig Boyd
Greenhorn

Joined: Aug 30, 2010
Posts: 21
Matthew, thanks for being patient with this green horn.

I think I made all the changes you suggested but now I am getting a different error when I try to compile the MyMenuBar class.

Here is all the code:


Here is the error:


What n00b error am I committing here?

Thanks,

Craigbert
Craig Boyd
Greenhorn

Joined: Aug 30, 2010
Posts: 21
OK. I got both classes compiling, but I think I am still creating two instances of the JMenuBar and the one I am getting is not the one I want.
Anyone have any idea where I am going astray?

MyJFrame.java :


MyMenuBar.java :



Thanks,

Craigbert
Certified LVI
(Local Village Idiot)
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3682
    
  16
You've gone back to how you had it originally (except your main method is now called menuBar).
Change menuBar into a constructor and remove the return statement. Constructors don't return anything.

Also you can remove lines 9 and 10. You don't need to create a JMenuBar.
Craig Boyd
Greenhorn

Joined: Aug 30, 2010
Posts: 21
THANK YOU Joanne!

That did it!!!

Thanks,

Craigbert
Certified LVI
(Local Village Idiot)
 
jQuery in Action, 2nd edition
 
subject: Creating one class from another