• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating one class from another

 
Craig Boyd
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is case sensitive. Try return
 
Craig Boyd
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne, thanks for the reply. I made the change and now I am getting:

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6337
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your return statement is outside your method.
 
Craig Boyd
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
 
Craig Boyd
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU Joanne!

That did it!!!

Thanks,

Craigbert
Certified LVI
(Local Village Idiot)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic