Easy Layout Question

A

AJ

I am trying to create an application that has a column of Components
on the leftsize, and a JPanel filling the entire right side of the
screen.

I have this working.

However, I am having trouble configuring a Layout Manager for the
JPanel containing the components on the left hand side.

This JPanel contains mainly buttons, but there will be one or two
JSliders, and maybe a JSpinner. I want all of these components to be
the same width and height.

I have tried a GridLayout, but the PreferredSize of the component is
ignored, and the components are drawn to fill the entire JPanel, which
is not what I want.

So I guess what I am asking is: which layout manager will allow me to
stack Components vertically in a JPanel, while respecting the
components preferred size?

BTW, I also tried BoxLayout, but it treated JButtons and JSliders
differently.
 
K

Karsten Lentzsch

AJ said:
I am trying to create an application that has a column of Components
on the leftsize, and a JPanel filling the entire right side of the
screen. [...]

The JGoodies FormLayout supports different measures
for the columns and rows and offers to give sets
of columns and rows the same width and height resp.
For the latter see the following methods:
FormLayout#setColumnGroups(int[][]) and
FormLayout#setRowGroups(int[][])

The Forms project is open source and available
at no charge. It is hosted at JavaDesktop.org, see
http://forms.dev.java.net

Screenshots and sample applications are here:
http://www.jgoodies.com/freeware/forms/

Although it seems that FormLayout can do your
design with a single layout container, you may
consider splitting it up into two parts: one
for the stacked components, one for the right
hand side. You can find a bunch of tips and tricks
around the Forms layout system in the accompanying
HTML documentation. Just open README.html.

Hope this helps,
Karsten Lentzsch :: JGoodies :: Java User Interface Design
 
C

Chris Smith

AJ said:
This JPanel contains mainly buttons, but there will be one or two
JSliders, and maybe a JSpinner. I want all of these components to be
the same width and height.

I have tried a GridLayout, but the PreferredSize of the component is
ignored, and the components are drawn to fill the entire JPanel, which
is not what I want.

Sounds as if you may want to nest a GridLayout inside a FlowLayout.
That is one way of respecting preferred size while keeping everything
the same size.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Ryan Stewart

I am trying to create an application that has a column of Components
on the leftsize, and a JPanel filling the entire right side of the
screen.

I have this working.

However, I am having trouble configuring a Layout Manager for the
JPanel containing the components on the left hand side.

This JPanel contains mainly buttons, but there will be one or two
JSliders, and maybe a JSpinner. I want all of these components to be
the same width and height.

I have tried a GridLayout, but the PreferredSize of the component is
ignored, and the components are drawn to fill the entire JPanel, which
is not what I want.

So I guess what I am asking is: which layout manager will allow me to
stack Components vertically in a JPanel, while respecting the
components preferred size?

BTW, I also tried BoxLayout, but it treated JButtons and JSliders
differently.

Layout managers are useful to a point, but if you want precise
positioning and sizing, you can use setLayout(null) on your JPanel and
setBounds(x, y, width, height) on your components.
 
K

Karsten Lentzsch

Ryan said:
[...] Layout managers are useful to a point,
but if you want precise positioning and sizing,
you can use setLayout(null) on your JPanel and
setBounds(x, y, width, height) on your components.

I strongly disagree. Multi-platform screen design
requires to honor the look, the fonts, font sizes,
screen resolution, language and platform guidelines.

Most layout managers take into account the look,
the font and font sizes, and the language. Only
a few layout systems honor the screen resolution
(96 vs. 120dpi) and platform guidelines (e. g.
Mac vs. Windows button order).

Pixel-based constant positions typically lead
to poore screen design; these work only if the
look is fixed, as well as all fonts, font sizes,
the screen resolution, language, and platform.

Best regards,
Karsten
:: JGoodies :: Java User Interface Design
 
R

Ryan Stewart

Karsten Lentzsch said:
Ryan said:
[...] Layout managers are useful to a point,
but if you want precise positioning and sizing,
you can use setLayout(null) on your JPanel and
setBounds(x, y, width, height) on your components.

I strongly disagree. Multi-platform screen design
requires to honor the look, the fonts, font sizes,
screen resolution, language and platform guidelines.

Most layout managers take into account the look,
the font and font sizes, and the language. Only
a few layout systems honor the screen resolution
(96 vs. 120dpi) and platform guidelines (e. g.
Mac vs. Windows button order).

Pixel-based constant positions typically lead
to poore screen design; these work only if the
look is fixed, as well as all fonts, font sizes,
the screen resolution, language, and platform.

Best regards,
Karsten
:: JGoodies :: Java User Interface Design

Well, I didn't say it was perfect, just that it works :) If you're not
doing anything too complex, it could be an acceptable and easy way out.
 
M

Mark Thornton

Ryan said:
Ryan Stewart wrote:

[...] Layout managers are useful to a point,
but if you want precise positioning and sizing,
you can use setLayout(null) on your JPanel and
setBounds(x, y, width, height) on your components.

I strongly disagree. Multi-platform screen design
requires to honor the look, the fonts, font sizes,
screen resolution, language and platform guidelines.

Most layout managers take into account the look,
the font and font sizes, and the language. Only
a few layout systems honor the screen resolution
(96 vs. 120dpi) and platform guidelines (e. g.
Mac vs. Windows button order).

Pixel-based constant positions typically lead
to poore screen design; these work only if the
look is fixed, as well as all fonts, font sizes,
the screen resolution, language, and platform.

Best regards,
Karsten
:: JGoodies :: Java User Interface Design


Well, I didn't say it was perfect, just that it works :) If you're not
doing anything too complex, it could be an acceptable and easy way out.

From the point of view of people with poor eyesight or other reasons to
run non standard screen resolution (or who have varied any of the other
aspects mentioned by Karsten), then pixel based layout DOES NOT WORK at all.

I have different screen resolution, different fonts and (slightly)
different platform to most of my co-workers. Thus for me a pixel based
layout is buggered three times over.

Mark Thornton
 
A

AJ

Chris Smith said:
Sounds as if you may want to nest a GridLayout inside a FlowLayout.
That is one way of respecting preferred size while keeping everything
the same size.

Interesting thought. Can you elaborate?
 
A

AJ

Karsten Lentzsch said:
AJ said:
I am trying to create an application that has a column of Components
on the leftsize, and a JPanel filling the entire right side of the
screen. [...]

The JGoodies FormLayout supports different measures
for the columns and rows and offers to give sets
of columns and rows the same width and height resp.
For the latter see the following methods:
FormLayout#setColumnGroups(int[][]) and
FormLayout#setRowGroups(int[][])

The Forms project is open source and available
at no charge. It is hosted at JavaDesktop.org, see
http://forms.dev.java.net

Screenshots and sample applications are here:
http://www.jgoodies.com/freeware/forms/

Although it seems that FormLayout can do your
design with a single layout container, you may
consider splitting it up into two parts: one
for the stacked components, one for the right
hand side. You can find a bunch of tips and tricks
around the Forms layout system in the accompanying
HTML documentation. Just open README.html.

Hope this helps,
Karsten Lentzsch :: JGoodies :: Java User Interface Design


Thanks Karsten. As my design stands now, there is a top-level JWindow
which houses 2 JPanels (one left, one right). The JPanel on the right
is using a CardLayout and works beautifully. The one on the left is
the one giving me problems. Thanks for your insight. From your
signature it appears your are affiliated with the JGoodies open source
project. I am going to test-drive your layout manager tomorrow to see
if it helps in my application. Thanks a bunch!

AJ
 
A

AJ

Layout managers are useful to a point, but if you want precise
positioning and sizing, you can use setLayout(null) on your JPanel and
setBounds(x, y, width, height) on your components.

Although I am having my troubles with LayoutManagers, I know that
using a fixed-position solution is not the way to go. For one, my
project requirement does not denote what kind of monitor I will be
running on. I don't even know if it will run on linux or unix!
Thusly, I must conform to a LayoutManager, no matter how annoying they
may be at times!
 
R

Ryan Stewart

Mark Thornton said:
Ryan said:
Ryan Stewart wrote:


[...] Layout managers are useful to a point,

but if you want precise positioning and sizing,
you can use setLayout(null) on your JPanel and

setBounds(x, y, width, height) on your components.

I strongly disagree. Multi-platform screen design
requires to honor the look, the fonts, font sizes,
screen resolution, language and platform guidelines.

Most layout managers take into account the look,
the font and font sizes, and the language. Only
a few layout systems honor the screen resolution
(96 vs. 120dpi) and platform guidelines (e. g.
Mac vs. Windows button order).

Pixel-based constant positions typically lead
to poore screen design; these work only if the
look is fixed, as well as all fonts, font sizes,
the screen resolution, language, and platform.

Best regards,
Karsten
:: JGoodies :: Java User Interface Design


Well, I didn't say it was perfect, just that it works :) If you're not
doing anything too complex, it could be an acceptable and easy way out.

From the point of view of people with poor eyesight or other reasons to
run non standard screen resolution (or who have varied any of the other
aspects mentioned by Karsten), then pixel based layout DOES NOT WORK at all.

I have different screen resolution, different fonts and (slightly)
different platform to most of my co-workers. Thus for me a pixel based
layout is buggered three times over.

Mark Thornton

I understand the problems it can generate, but I still say if you only need
it for a few small buttons or something like that, it can be worked with.
Since AJ has said he can't use it though, further discussion seems
pointless.
 
K

Karsten Lentzsch

Karl said:
I would use a GridBagLayout. It's a bit complex, but gives you a great
deal of control, without having to resort to absolute pixel values. I
think it's worth learning:

Many developers are not aware of the fact that the
GridBagLayout lacks essential screen design features.
I've summarized some of them in a short presentation:
http://www.jgoodies.com/articles/layout-essentials.pdf

From the GBL's complexity, it doesn't follow that
it can implement complex or even simple layouts.
For example, with the GBL you cannot implement a single
button bar that follows the MS layout style guidelines.
(And these make sense). The Forms Demo shows several
layout tasks that you cannot implement with the GBL
or combinations of GBL with GridLayout, TableLayout.
Only SpringLayout could potentially implement the examples
shown - if you would add a bunch of custom springs. See
http://www.jgoodies.com/freeware/formsdemo/

A more thorough discussion of layout problems and good
and poor Java design can be found in the Forms whitepaper.
http://www.jgoodies.com/articles/
If you favor pictures over text, see the presentations.

Hope this helps. Best regards,
Karsten
 
A

AJ

I would use a GridBagLayout. It's a bit complex, but gives you a great
deal of control, without having to resort to absolute pixel values. I
think it's worth learning:
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html

Karl,

I did end up implementing a GridBagLayout solution. It is almost
done. Except that the components are centered vertically in the
enclosing JPanel rather than being aligned at the top. Some ascii art
might help here:

This is a pseudo-toolbar on the left-hand side of my app.

Here is what I have:

______
| |
| |
| |
|______|
|BUTTON|
|------|
|BUTTON|
|------|
| |
| |
| |
|______|


This is what I want:


______
|BUTTON|
|------|
|BUTTON|
|------|
| |
| |
| |
| |
| |
| |
| |
|______|


I tried setting the anchor field to NORTH in the JPanel, to no avail.
Can anyone help?

Thanks.
 
S

Sudsy

AJ wrote:

Karl,

I did end up implementing a GridBagLayout solution. It is almost
done. Except that the components are centered vertically in the
enclosing JPanel rather than being aligned at the top. Some ascii art
might help here:
I tried setting the anchor field to NORTH in the JPanel, to no avail.
Can anyone help?


Add an empty JPanel child with gridheight = GridBagContraints.REMAINDER
and weighty = 0.5 (actually anything above 0) as the constraints.
 
A

A. Bolmarcich

AJ wrote:





Add an empty JPanel child with gridheight = GridBagContraints.REMAINDER
and weighty = 0.5 (actually anything above 0) as the constraints.

As an alternative to adding an empty JPanel, have the GridBagConstraints
of the last component be: weighty > 0, anchor = GridBagConstraints.NORTH,
and fill = GridBagConstraints.NONE.
 
S

Sudsy

A. Bolmarcich wrote:

As an alternative to adding an empty JPanel, have the GridBagConstraints
of the last component be: weighty > 0, anchor = GridBagConstraints.NORTH,
and fill = GridBagConstraints.NONE.

That'll work but it should also be noted that NONE is the default for
fill and you might want to use HORIZONTAL if the button labels give
them differents widths.
As always, experimentation is the best teacher...
 
N

natG

AJ said:
I am trying to create an application that has a column of Components
on the leftsize, and a JPanel filling the entire right side of the
screen.

I have this working.

However, I am having trouble configuring a Layout Manager for the
JPanel containing the components on the left hand side.

This JPanel contains mainly buttons, but there will be one or two
JSliders, and maybe a JSpinner. I want all of these components to be
the same width and height.

I have tried a GridLayout, but the PreferredSize of the component is
ignored, and the components are drawn to fill the entire JPanel, which
is not what I want.

So I guess what I am asking is: which layout manager will allow me to
stack Components vertically in a JPanel, while respecting the
components preferred size?

BTW, I also tried BoxLayout, but it treated JButtons and JSliders
differently.

I have been lookin at this thread and can't seem to understand why all the
complexity.
So before posting this article, I actually tried it and it works.
If I understand correctly, you want a couple of components on the left at the top
(per your diagram).
1. For the left panel (leftPanel) use a BorderLayout.
2. Create another panel to be used inside the left panel, and add all the Buttons
you want "stuck on top" to it.
3. Then simply add this inner panel to the left panel,
leftPanel.add(innerPanel,BorderLayout.NORTH).
voila!

-nat
 
S

Sudsy

natG wrote:

I have been lookin at this thread and can't seem to understand why all the
complexity.
So before posting this article, I actually tried it and it works.
If I understand correctly, you want a couple of components on the left at the top
(per your diagram).
1. For the left panel (leftPanel) use a BorderLayout.
2. Create another panel to be used inside the left panel, and add all the Buttons
you want "stuck on top" to it.
3. Then simply add this inner panel to the left panel,
leftPanel.add(innerPanel,BorderLayout.NORTH).
voila!

The beauty of layout managers is that they can do so much for you,
once you learn their strengths. You can also nest elements and
use different managers for their respective containers. If all
else fails, it's not too onerous to "roll your own": just implement
the java.awt.LayoutManager2 interface.
But as I mentioned previously, playing around with the different
managers using different attributes is arguably most educational.
There's more than one way to combine elements in order to achieve
the same goal (kind of like *nix).
Remember, you can't make an omelette without cracking a few eggs.
 
K

Karl von Laudermann

Karl,

I did end up implementing a GridBagLayout solution. It is almost
done. Except that the components are centered vertically in the
enclosing JPanel rather than being aligned at the top. Some ascii art
might help here:

Simple. Set the weighty of the top component to 0, and the weighty of
the bottom component to something positive, like 0.5. This will make
the bottom control take up all the empty space, and with anchor set to
NORTH it will sit at the top of the empty space (as long as fill is
not set to VERTICAL).

If all of the weights are 0, then the controls will all clump together
in the center as you are seeing.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top