This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes 1 JFrame containing Two JPanels... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "1 JFrame containing Two JPanels..." Watch "1 JFrame containing Two JPanels..." New topic

1 JFrame containing Two JPanels...

Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
It should come pretty easy, but it's not making sense why I cannot have a 500x300 pix frame
and inside it, have two 250x500 JPanels (side by side). I was hard at it for about a week and didn't even come close. I cannot even add a small JPanel to a JFrame without the panel taking up the whole frame.
In the following code, how can I add two TSPanels (side by side) to occupy one JFrame???

Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Hi Steven,
A Frame's default layout is Border Layout, so if u dont specify any layout, it just adds components in the CENTER ( like whats happening in your case ), so what u can do is specify BorderLayout & add the 2 panels in CENTER & EAST/WEST , or use FlowLayout & maximize the Panels.
Also I changed maximum size to preferred size.
this is ur modified code:
import java.awt.*;
import javax.swing.*;
class TSPanel extends JPanel
public TSPanel ()
this.setBackground (;
Dimension d1 = new Dimension(250, 150);
public void paintComponent (Graphics g)
g.drawString ("Hello World!", 100, 100);
public class ts4copy
public static void main (String[] argv)
JFrame frame = new JFrame ();
frame.setTitle ("Hello World Test");
frame.setResizable (true);
frame.setSize (500, 300);
TSPanel panel = new TSPanel ();
TSPanel panel1 = new TSPanel ();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add (panel,BorderLayout.CENTER);
frame.getContentPane().add (panel1,BorderLayout.WEST);
frame.setVisible (true);

Should do it!

- Vinod<br />-------<br />SCJP2
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
Thanks alot! I've been going batty trying to accomplish that very same thing. Looking back, I know I've come close more than once but I completely overlooked the layout managers. Thanks, Vinod, for getting me unstuck.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
GridLayout would work very well for this. Just create a grid with 1 row and 2 columns and place your panels in the grid.
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
That makes sense...gridlayout it is. Thanks, guys, for helping me out and also renewing my enthusiasm. SteveII
I agree. Here's the link:
subject: 1 JFrame containing Two JPanels...
It's not a secret anymore!