confused about layout managers


G

Guest

I have nearly finished my Applet and I'm now left only to finalize the
layout. So far I've used a GridLayout(3, 1) but now want to change to
something else. I played around with a GridBagLayout() but I'm getting
unexpected resizing effects.

My applet consists of 3 areas in a single column, from top to bottom :

a) a tab pane
b) a JLabel wrapped in a ScrollPane used as message area
c) a scrollPane originally wrapped around a dummy Jlabel, and
later dynamically removed and replaced by a scrollPane wrapped
around a JTable

The first tab of the tab pane contains a ScrollPane wrapped around a
customized "myDisplay extends JPanel implements Scrollable", the other
tabs are just static control menus.

With the test grid layout the 3 areas a/b/c are (of course) of the same
vertical size.

The myDisplay in (a) is initialized to a 256x256 ramp image (which
appears to be definitely narrower than the applet width, and slightly
not tall enough to contain the image, so a vertical scroll bar appears.

Later I load images in it, which can be smaller or larger. They can
also be zoomed 2x, 3x, 4x etc. so they will soon become larger and
scroll bars appear as appropriate.

(b) is initialized to an empty area, messages appear in it and scroll
bar appears when it's full

(c) as I said is initialized to a "nothing loaded" message. As soon
as I load an image in (a) a table of "regions" is loaded in (c), which
can have more or less rows, therefore causing a scroll bar to appear or
not.

The only component for which a setPreferredSize is set is the myDisplay
inside the scroll pane in the first tab of (a).

In the test configuration (GridLayout(3, 1)) everything goes fine (the
three pieces a/b/c are never auto-resized, ... scroll bars in them
appear when appropriate ...

.... but I'm NOT SATISFIED because the three pieces have the same height.

What I'd wish to achieve is 3 components of DIFFERENT height (say that
I'd wish (a) to be tall enough to contain a 512x512 image plus the tabs,
(b) to be only 3-4 text lines, and (c) to occupy the rest of the page,
a bit less than the height of (a).

I tried with GridBagLayout, but that has SIDE EFFECTS ... as the various
components dynamically resize. Sometimes the myDisplay in (a) resizes
and loses the scrollbars, and this pushes the bottom of the Jtable in
(c) offscreen. While the message area in (b) grows higher than wished
and then remains like that.

This is the latest set of GridBagConstrains I tried (c1 c2 c3 for the
components (a) (b) (c) from top to bottom), but I tried several
variations.

c1.gridx=0 ; c1.gridy=0 ;
c1.gridwidth=GridBagConstraints.REMAINDER ;
c1.gridheight=10 ;
c1.fill=GridBagConstraints.BOTH ;
c1.anchor=GridBagConstraints.PAGE_START;
c1.weightx=1.0 ; c1.weighty=0.0 ;
c2.gridx=0 ; c1.gridy=11 ;
c2.gridwidth=GridBagConstraints.REMAINDER ;
c2.gridheight=5 ;
c2.fill=GridBagConstraints.BOTH ;
c2.anchor=GridBagConstraints.LINE_END ;
c2.weightx=1.0 ; c2.weighty=0.0 ;
c3.gridx=0 ; c1.gridy=16 ;
c3.gridwidth=GridBagConstraints.REMAINDER ;
c3.gridheight=GridBagConstraints.REMAINDER ;
c3.fill=GridBagConstraints.BOTH ;
c3.anchor=GridBagConstraints.PAGE_END;
c3.weightx=1.0 ; c3.weighty=0.0 ;

How can I achieve what I want i.e. three vertical sections of FIXED
DIFFERENT heights, which never resize, but in which scroll bars appear
when needed ?
 
Ad

Advertisements

K

Knute Johnson

LC's No-Spam Newsreading account said:
I have nearly finished my Applet and I'm now left only to finalize the
layout. So far I've used a GridLayout(3, 1) but now want to change to
something else. I played around with a GridBagLayout() but I'm getting
unexpected resizing effects.

My applet consists of 3 areas in a single column, from top to bottom :

a) a tab pane
b) a JLabel wrapped in a ScrollPane used as message area
c) a scrollPane originally wrapped around a dummy Jlabel, and
later dynamically removed and replaced by a scrollPane wrapped
around a JTable

The first tab of the tab pane contains a ScrollPane wrapped around a
customized "myDisplay extends JPanel implements Scrollable", the other
tabs are just static control menus.

With the test grid layout the 3 areas a/b/c are (of course) of the same
vertical size.

The myDisplay in (a) is initialized to a 256x256 ramp image (which
appears to be definitely narrower than the applet width, and slightly
not tall enough to contain the image, so a vertical scroll bar appears.

Later I load images in it, which can be smaller or larger. They can
also be zoomed 2x, 3x, 4x etc. so they will soon become larger and
scroll bars appear as appropriate.

(b) is initialized to an empty area, messages appear in it and scroll
bar appears when it's full

(c) as I said is initialized to a "nothing loaded" message. As soon
as I load an image in (a) a table of "regions" is loaded in (c), which
can have more or less rows, therefore causing a scroll bar to appear or
not.

The only component for which a setPreferredSize is set is the myDisplay
inside the scroll pane in the first tab of (a).

In the test configuration (GridLayout(3, 1)) everything goes fine (the
three pieces a/b/c are never auto-resized, ... scroll bars in them
appear when appropriate ...

... but I'm NOT SATISFIED because the three pieces have the same height.

What I'd wish to achieve is 3 components of DIFFERENT height (say that
I'd wish (a) to be tall enough to contain a 512x512 image plus the tabs,
(b) to be only 3-4 text lines, and (c) to occupy the rest of the page, a
bit less than the height of (a).

I tried with GridBagLayout, but that has SIDE EFFECTS ... as the various
components dynamically resize. Sometimes the myDisplay in (a) resizes
and loses the scrollbars, and this pushes the bottom of the Jtable in
(c) offscreen. While the message area in (b) grows higher than wished
and then remains like that.

This is the latest set of GridBagConstrains I tried (c1 c2 c3 for the
components (a) (b) (c) from top to bottom), but I tried several variations.

c1.gridx=0 ; c1.gridy=0 ;
c1.gridwidth=GridBagConstraints.REMAINDER ;
c1.gridheight=10 ;
c1.fill=GridBagConstraints.BOTH ;
c1.anchor=GridBagConstraints.PAGE_START;
c1.weightx=1.0 ; c1.weighty=0.0 ;
c2.gridx=0 ; c1.gridy=11 ;
c2.gridwidth=GridBagConstraints.REMAINDER ;
c2.gridheight=5 ;
c2.fill=GridBagConstraints.BOTH ;
c2.anchor=GridBagConstraints.LINE_END ;
c2.weightx=1.0 ; c2.weighty=0.0 ;
c3.gridx=0 ; c1.gridy=16 ;
c3.gridwidth=GridBagConstraints.REMAINDER ;
c3.gridheight=GridBagConstraints.REMAINDER ;
c3.fill=GridBagConstraints.BOTH ;
c3.anchor=GridBagConstraints.PAGE_END;
c3.weightx=1.0 ; c3.weighty=0.0 ;

How can I achieve what I want i.e. three vertical sections of FIXED
DIFFERENT heights, which never resize, but in which scroll bars appear
when needed ?

Use GridBagLayout and set the gridx constraint to 0 for all three
components. Set the preferred sizes of the containers to what you want
them to be.
 
G

Guest

LC's No-Spam Newsreading account wrote:

Use GridBagLayout and set the gridx constraint to 0 for all three components.

gridx was already set to zero as it was visible from the code I
included. I'm confused about whether the rest of the code is of any real
use.
Set the preferred sizes of the containers to what you want them to be.

Thanks, that did the trick.
It's the containers that matter !
 
K

Knute Johnson

LC's No-Spam Newsreading account said:
gridx was already set to zero as it was visible from the code I
included. I'm confused about whether the rest of the code is of any real
use.

What I should have said was only set gridx to 0 and forget all the rest.
Thanks, that did the trick.
It's the containers that matter !

Excellent!
 
Ad

Advertisements

Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top