• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

1 JFrame containing Two JPanels...

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense...gridlayout it is. Thanks, guys, for helping me out and also renewing my enthusiasm. SteveII
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!