• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding GUI components to parent components

 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on my GUI to which I will add functionality later.
Using about 4 different examples, the API, and tuts from sun.

For the life of me I can't figure out how to add components from another class.

I want a frame as a class and add a menu bar, and other components inside the frame, but want to work to keep everything as seperate as I can. Currently in many of the examples I can find, I see that elements like the menu bar is added to the JFrame in the JFrame class. I'd rather design each piece in its own class and make it seperate.

How oh how do I add these components from other classes. Trust me, I've tried everything.

I thought it would be
frame.add(selectcomponent);
but I can't figure out the call for that.
yeah, I know its trivial. Humor me.

also, when adding tabbed panes, why am I getting an error on this. Its used in an example but is causing me problems. According to the format in the API, this makes sense and should work. I have created an empty Instructions class to eliminate any errors with the class not existing, but I can't figure out why the error.
tabbedPane.addTab("Instructions", null, new Instructions(), "Get instructions here");

JRE 1.5
using Eclipse 3.0.1
Thanks for any guidance.
 
Aabha Varma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to create a class for each component, then you need to extend the super class.

say creating a button,,,
class SampleButton extends JButton..

And then you add this to container.
panel.add(new SampleButton());

To answer to the second part, if you want to add to the frame you need to get the contentpane and then add the components.

Hope this answers
 
Snoop One
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt,
Well it depends on where you place the responsibility? You can not escape the fact that somewhere in the application a class (or a set of classes) must assemble the GUI.
So if you do not want to use inheritance (most of the sun tut. do this) you can just use composition. An example is listed below.



In case of inheritance, do not use it unless you want to change the behavior of the parent or if you have an urge to use polymorphic variables.

Aabha: you can actually use frame.add() method without getting the content pane in JAVA 1.5
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!