JPanel doenst grow  
Author Message
Wouterrrr via JavaKB.com





PostPosted: 2005-3-8 23:55:00 Top

java-programmer, JPanel doenst grow Hey, I am beginning Java Student with a Java Problem. I looked on the
Internet but didn't find my answer anywhere, maybe you guys could help ?

I have a:

JFRAME --> Container+GridBayLayout

Container+GridBayLayout --> JPanel options
--> JScrollpanel

JScrollpanel --> add(new RectangleGroup());

//A piece of the entire class RectangleGroup:
-------------------
public class RectangleGroup extends JPanel
{
public void paintComponent(Graphics g)
{
this.g = g;

super.paintComponent(g);

//A lot of THESE Things
g.drawRect(150,100,100,10);
}
}
-------------------
The problem is that I dont get a scrollbar when I draw a rectangle that is
out of sight in the browser.

A have uploaded a picture of it here:
http://www.denbreejen.net/public/School/dnaproject2/browser_problem.gif

Wouter

--
Message posted via http://www.javakb.com
 
klynn47





PostPosted: 2005-3-9 0:53:00 Top

java-programmer >> JPanel doenst grow You need to reset the preferred size of RectangleGroup and call
revalidate

 
Wouterrrr via JavaKB.com





PostPosted: 2005-3-9 8:51:00 Top

java-programmer >> JPanel doenst grow It tried this:
rect_group_instance.preferredSize();
rect_group_instance.revalidate();

But that dind't work

But this for instance:
rect_group_instance.setPreferredSize(new Dimension(0, 2000));
Does size up my JPanel, but it has to be done manually, which is not what I
want.

Wouter

--
Message posted via http://www.javakb.com
 
 
John McGrath





PostPosted: 2005-3-9 20:10:00 Top

java-programmer >> JPanel doenst grow On 3/8/2005 at 7:51:00 PM, Wouterrrr via JavaKB.com wrote:

> It tried this:
> rect_group_instance.preferredSize();
> rect_group_instance.revalidate();
> But that dind't work

The preferredSize() method does not *set* the preferred size. It is the
deprecated equivalent of getPreferredSize(), which not surprisingly,
*gets* the preferred size.

> But this for instance:
> rect_group_instance.setPreferredSize(new Dimension(0, 2000));
> Does size up my JPanel, but it has to be done manually, which is not
> what I want.

Your RectangleGroup component needs to be able to tell the world how big
it wants to be. If it does not do that, then it will probably not be set
to the size that it wants. It should not be hard to figure out the
preferred size. The paintComponent() method is painting in a given area,
so that should be sufficient to determine the preferred size.

--
Regards,

John McGrath
 
 
Wouterrrr Wouterrrr via JavaKB.com





PostPosted: 2005-3-9 23:49:00 Top

java-programmer >> JPanel doenst grow "It should not be hard to figure out the preferred size."

so I will have to calculate in my RectangleGroup class how big it is/will
be, and then give that to the setPreferredSize() function ??

So if this is the only rectangle:
g.drawRect(150,180,100,10);

Then that one is on y: 180 + height: 10 = 190

So I should set:

setPreferredSize(new Dimension(0, 190));
so I can see the whole rectangle ?

Wouter

--
Message posted via http://www.javakb.com
 
 
John McGrath





PostPosted: 2005-3-10 3:55:00 Top

java-programmer >> JPanel doenst grow On 3/9/2005 at 10:49:06 AM, Wouterrrr Wouterrrr via JavaKB.com wrote:

> so I will have to calculate in my RectangleGroup class how big it is/will
> be, and then give that to the setPreferredSize() function ??

Exactly. If you know the size before the component is realized (shown),
then you can just call setPreferredSize(). If the size changes after the
component is realized, you will probably need to revalidate() it.

> So if this is the only rectangle:
> g.drawRect(150,180,100,10);
>
> Then that one is on y: 180 + height: 10 = 190
>
> So I should set:
>
> setPreferredSize(new Dimension(0, 190));
> so I can see the whole rectangle ?

That is the correct height, but if the component width is 0, it will be
pretty hard to paint anything in it. In your scenario, the width of the
component should be (x + width) = (150 + 100) = 250.

--
Regards,

John McGrath