File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem laying out panels in an Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem laying out panels in an Applet" Watch "Problem laying out panels in an Applet" New topic

Problem laying out panels in an Applet

Neil Guerin

Joined: Mar 16, 2004
Posts: 2
Hi. I'm trying to lay out panels in a test applet shown below. I want to have two panels each with a different layout manager. I want the first panel to always display above the other one when the applet is launched. At present, the second panel appears directly after the first panel. Is there anyway I can get around this so I can achieve the layout I want? I did get it working using the JApplet class but I don't like the look and feel of that -- I'm doing early gui testing for an applet to mimic a standard html web form for a college project and the Applet class is the one I have to use.
I also tried putting the two panels in a single container and applying a border layout to both panels when added to the container (north and south respectively). Although the file compiles, nothing displays in the Applet when I run the appletviewer when attempting that approach. It just says Applet initiated and remains a blank window).
Here is the source code for the one that does display but not the way I'd like: would really appreciate any tips or web references anybody can provide. I've looked at Sun's tutorials and my textbook but they all use JApplet examples although they do say it should be possible to position panels too as they are containers. Let me know if I have that wrong. Thanks for any help in advance.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class NetApplet4 extends Applet
private TextField message = null ;
private Button button = null;
private TextField message2 = null ;
private Button button2 = null;
private boolean pressed = false;

// extra test variables
private Button button3 = null;
private TextField message3 = null;
private Button button4 = null;
private TextField message4 = null;
public NetApplet4()
public void init()
// set default window size
resize(750, 400);
// declare a new panel
Panel pnlDisplay1 = new Panel();

// assign a layout manager to the panel
pnlDisplay1.setLayout(new GridLayout(0,2));

// instantiate objects for panel 1
button = new Button("PRESS ME");
message = new TextField(30);
button3 = new Button("Button 3");
message3 = new TextField("Message 3");
button4 = new Button("Button 4");
message4 = new TextField("Message 4");
// add objects to panel 1

// add the panel1
add(pnlDisplay1, BorderLayout.NORTH);
// declare a new panel
Panel pnlDisplay2 = new Panel();

// assign a layout manager to the panel
pnlDisplay2.setLayout(new FlowLayout());
// instantiate objects for panel 2
button2 = new Button("Submit Ticket Order Online");
// add objects to panel 2
// add the panel2
add(pnlDisplay2, BorderLayout.SOUTH);

} // end NetApplet4 class
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Neil Guerin

Joined: Mar 16, 2004
Posts: 2
Have just tested this code you provided and it does exactly what I was looking for. Thank you so much for your help. Really appreciate it.
I agree. Here's the link:
subject: Problem laying out panels in an Applet
It's not a secret anymore!