• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting panels the size I want without using IDE  RSS feed

 
Colm Dickson
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.


Why is it that when I add panels of a set size to a frame container of a set size, that the panels just fill the size of the panel and do not take on the dimension I give them? I want to achieve this without using the IDE please. As an example, I include some simple code below. Can anyone point out to me why the panel sizing has not affected the resulting panels

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;




Thanks,

Colm
 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using a LayoutManager (which is a good thing), but most of those ignore the size you've set using setSize. Try setPreferredSize instead.
 
Colm Dickson
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying.

I tried the same code but replace the setSize with setPreferredSize using a dimension instance set to the size I wanted



but the end result was the same. The panels still took up the same space as he main frame.

Colm.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The frame's content pane defaults to BorderLayout which does not respect the user defined sizes. Set it to FlowLayout and then try adding the panels.
 
Colm Dickson
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much...that worked, I can now see the benefit of adding panels to panels to get things looking how I want them.


Colm.

 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also be using frame.pack() if you want the layout managers to repect the preferred size.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!