• Post Reply Bookmark Topic Watch Topic
  • New Topic

Way to reliably set the size of a JPanel in a JFrame?  RSS feed

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys/gals,

I've done some reading, and apparently setPreferredSize() is better than setSize() for sizing JPanels. However, when I try the following with my JPanel, it sets the panel size to 1, 1, which is basically invisible. It also happens when I used setSize() instead of setPreferredSize(). Is there a way to control the size of the JPanel? I'm using a GridLayout on the JFrame to place my JPanels (using GridLayout(0, 3) as per Rob's suggestion). Can anyone provide any advice or insight? Thank you in advance!


 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on the LayoutManagers being used. The layout manager uses the preferred size as a suggestion. Then based on the rules of the layout manager it sets the size and location of the components.

In your case a GridLayout will use all the space available and just divide it up 3 ways so you may not get the layout you expect. But based on 4 lines of code we can't tell what you are doing.

Also, the only time you should be referencing a Graphics object is in the paintComponent() method.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it seems to happen even with flowLayout(), and even with no layout (null), and just placing the panels via panel.setLocation(x, y). The size of the panel is 1, 1, as verified by a System.out statement which displays the idmensions of the panel. OK, so the assignment is to draw a user-specified numer of squares or circles on a panel. drawRect() and drawOval() both require Graphics2D objects, and I believe even drawLine() requires a Graphics object. How should I do this without a Graphics or Graphics2D object (no smarta$$ attitude intended; this is a genuine question)? I've tried to narrow the project down some, but the classes are probably too tightly coupled for comfort (another programming flaw of mine), so I can't trim too much without causing problems:

Main class:


DrawingPanel class:


ColorPanel class:


NumberPanel class:

ShapePanel class:
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should I do this without a Graphics or Graphics2D object (no smarta$$ attitude intended;


I didn't say you shouldn't use a Graphics object. Read my comment!

I can't trim too much without causing problems:


You said all the drawing are causing problems so there is no need to include all 4 custom drawing objects. First you get it working with one drawing panel, then 2, then 3. Thats how you start with a small SSCCE and then build on it. So yes you can make it smaller. I'm not about do download all those individual classes.

Custom Painting Approaches is my attempt at a simple SSCCE. It also include a link to the Swing tutorial on custom painting.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah-hah, I got it! I was accessing the this.HEIGHT and this.WIDTH fields for dimension check, instead of this.getHeight() and this.getWidth(). Upon changing, I discovered that my pane was indeed 300 x 300 So, when you say 'referencing a graphics object', what exactly do you mean (vs. using a graphics object)? Graphics g = this.getGraphics(), I assume?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!