This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Border Layout in Applet Issues Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Border Layout in Applet Issues" Watch "Border Layout in Applet Issues" New topic
Author

Border Layout in Applet Issues

Jeff Hord
Greenhorn

Joined: Apr 28, 2011
Posts: 2
I am a new programmer and doing an assignment for my Java class. I think I am using the right layout, but the components aren't showing up right. They show up in a line all across the top of the applet. I am using the BorderLayout. Please let me know what I am doing wrong. Thank you!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35443
    
    9
With Swing you don't call "add()" or "setLayout(...)" directly, you'd call those methods on the content pane, like "getContentPane ().add(...)".

I'll move this to the AWT/Swing forum, as there seems nothing applet-specific to the question.


Android appsImageJ pluginsJava web charts
Jeff Hord
Greenhorn

Joined: Apr 28, 2011
Posts: 2
Ulf Dittmer wrote:With Swing you don't call "add()" or "setLayout(...)" directly, you'd call those methods on the content pane, like "getContentPane ().add(...)".

I'll move this to the AWT/Swing forum, as there seems nothing applet-specific to the question.



Then I must just be doing it completely wrong. I am trying to make an applet.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35443
    
    9
What I meant by that is: the question is about layouts, and thus GUIs and Swing in general - which are the same for both applications and applets. The fact that this code is used in an applet makes no difference.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8566

Jeff Hord wrote:
Then I must just be doing it completely wrong. I am trying to make an applet.

JApple is a Swing component too. So the same concepts apply to applets.
Check out http://download.oracle.com/javase/tutorial/uiswing/components/applet.html
Notice the line getContentPane().add(label, BorderLayout.CENTER);

[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1808
    
    2
With Swing you don't call "add()" or "setLayout(...)" directly, you'd call those methods on the content pane, like "getContentPane ().add(...)".


You don't need getContentPane(). JDK 5 supports this. These methods of a JFrame are directed to the content pane of the frame.

Please let me know what I am doing wrong


The code provided looks fine, so the problem is somewhere else.

Post your SSCCE that demonstrates the problem. And we don't care about your real panels. Just create a simple program that adds 5 different components to each area of the BorderLayout. So your SSCCE should basically be 5 lines of code in the init() method:



There is no need to set the layout specifically since a BorderLayout is used for all top level containers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Rob Camick wrote:
With Swing you don't call "add()" or "setLayout(...)" directly, you'd call those methods on the content pane, like "getContentPane ().add(...)".


You don't need getContentPane(). JDK 5 supports this. These methods of a JFrame are directed to the content pane of the frame.

We're talking about JApplet here, but you're right - since Java 5.0 all of JFrame, JDialog and JApplet redirect calls to add, remove and setLayout to their content pane.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Border Layout in Applet Issues
 
Similar Threads
Problem laying out panels in an Applet
layout manager question
Swing: Unable to bring down the size of JPanel
Having problems making a simple GUI
applet question