Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

Java Swing

Thread view: 
Nuno Silva - 28 Apr 2005 16:21 GMT
Hi, I'm pretty new to Swing, and I'm at the hello world example code from
java.sun.com. I'm trying to add next to the JLabel a JButton, and next to
frame.getContentPane().add(label); I do the same for the JButton created,
but in the GUI only shows the button. Can you explain to me why this happens
and why not appending the button next to the label? And how should I do to
make them put together?

Thanks in advance.

Nuno
Bjorn Abelli - 28 Apr 2005 21:02 GMT
"Nuno Silva" ...

> Hi, I'm pretty new to Swing, and I'm at the hello
> world example code from java.sun.com.

Your problem would be no different in AWT...

> I'm trying to add next to the JLabel a JButton,
> and next to frame.getContentPane().add(label);
[quoted text clipped - 3 lines]
> and why not appending the button next to
> the label?

The default LayoutManager in a Frame/JFrame is BorderLayout, that has 5
areas, in which only one component can exist at one time.

 frame.getContentPane().add(label);

...is "equal" to:

 frame.getContentPane().add(label, BorderLayout.CENTER);

So when you "add" the button, it actually *replaces* the label.

> And how should I do to
> make them put together?

There's a lot of possibilities.

You could start with changing the "adding" to actually use different areas,
e.g.

 frame.getContentPane().add(label, BorderLayout.CENTER);
 frame.getContentPane().add(button, BorderLayout.SOUTH);

...or you could change the LayoutManager to something else than a
BorderLayout before adding the components:

 frame.getContentPane().setLayout( new FlowLayout() );

 frame.getContentPane().add(label);
 frame.getContentPane().add(button);

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/BorderLayout.html
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/FlowLayout.html

BTW, if you're using the latest version (1.5), there's no longer a need for
the "getContentPane()", as you can use "add" directly on the JFrame (which
forwards it to the pane).

// Bjorn A


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.