This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "1 JFrame containing Two JPanels..." Watch "1 JFrame containing Two JPanels..." New topic
Author

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 (Color.blue);
Dimension d1 = new Dimension(250, 150);
//this.setMaximumSize(d1);
this.setPreferredSize(d1);
}
public void paintComponent (Graphics g)
{
super.paintComponent(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


- 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.
SteveII
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: http://aspose.com/file-tools
 
subject: 1 JFrame containing Two JPanels...
 
Similar Threads
Liquid LayoutManager
Haw to display painting in Panel
Paint Problem
string concatenation
access JPanel s paint-method