Newbie Question: Layouts  
Author Message
Debaser





PostPosted: 2004-11-12 2:00:00 Top

java-programmer, Newbie Question: Layouts I've been having the toughest time with this....
I want to have a JFrame with 3 containers in it.
The containers are laid out top to bottom.
Inside the first 2 containers I want 4 rows of a JLabel and
JTextField. Components are lined up in both containers so that the
labels end and textfields start at the same place on each line. These
two containers also have a titled borders, as does the third container
which is just a JTextArea. My questions are: what type should these
containers be? what type of layout should I use for them (and what
should the parameters to the method be if any)?

Thanks.

 
Andrew Thompson





PostPosted: 2004-11-12 2:11:00 Top

java-programmer >> Newbie Question: Layouts On Thu, 11 Nov 2004 12:59:34 -0500, Debaser wrote:

> I've been having the toughest time with this....
> I want to have a JFrame with 3 containers in it.
> The containers are laid out top to bottom.
> Inside the first 2 containers I want 4 rows of a JLabel and
> JTextField. Components are lined up in both containers so that the
> labels end and textfields start at the same place on each line. These
> two containers also have a titled borders, as does the third container
> which is just a JTextArea. My questions are: what type should these
> containers be? what type of layout should I use for them (and what
> should the parameters to the method be if any)?

One way to achieve the basic effect you are after is
a nested layout. The outermost layout would be a
BorderLayout, that would contain your JTextArea in SOUTH.

The top two areas would be in JPanels with another layout
in the main NORTH and CENTER areas. Each JPanel would have
a layout and/or other panels to further divide it. As best I
understand your description, a further BorderLayout with the
JLabels in a GridLayout in WEST, while the JTextFields are in
a GridLayout in EAST.

For more ideas, see the Java Tutorial on lyout managers..
<http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html>

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Fahd Shariff





PostPosted: 2004-11-12 2:17:00 Top

java-programmer >> Newbie Question: Layouts Use a gridlayout (at the root) with 3 rows and 1 column. To this add
your three containers.

The first two containers could have another grid layout with 4 rows and
2 columns each. To this simply add your jlabel, textfield pair. The
third container would have a BorderLayout to which you add a JTextArea
in the center. To each container, borders can be added.

The containers could just be JPanels with the layouts set as explained
above.

JPanel rootP = new JPanel() ;
rootP.setLayout(new GridLayout(0,1));

//now build the 3 containers:
JPanel c1 = new JPanel() ;
c1.setLayout(new GridLayout(0,2)) ;
c1.setBorder(.....) ;
c1.add(new JLabel("label1")) ;
c1.add(new JTextField()) ;
//and so on
//remember that the number of columns are 2 and the number of rows
increases dynamically as we add stuff

JPanel c3 = new JPanel(new BorderLayout()) ;
JScrollPane scroll = new JScrollPane(new JTextArea()) ;
c3.add(scroll,BorderLayout.CENTER) ;

rootP.add(c1) ;
rootP.add(c2) ;
rootP.add(c3) ;
//add rootP to the contentPane of the JFrame:
f.getContentPane().add(rootP) ;

The code above may have errors! I have written it on the fly, so please
try to make sense of it!

Hope this helps,

 
 
Jaap de Bergen





PostPosted: 2004-11-12 4:43:00 Top

java-programmer >> Newbie Question: Layouts On Thu, 11 Nov 2004 12:59:34 -0500, Debaser <email***@***.com> wrote:

>I've been having the toughest time with this....
>I want to have a JFrame with 3 containers in it.
>The containers are laid out top to bottom.
>Inside the first 2 containers I want 4 rows of a JLabel and
>JTextField. Components are lined up in both containers so that the
>labels end and textfields start at the same place on each line. These
>two containers also have a titled borders, as does the third container
>which is just a JTextArea. My questions are: what type should these
>containers be? what type of layout should I use for them (and what
>should the parameters to the method be if any)?
>

I'm using formlayout from jgoodies, this layoutmanager can solve most
layout problems. It wise with formlayout to make a quick drawing on
paper so you can draw the rows and columns. After you have done that,
you can make your gui easily with formlayout. I must warn you the
definition of the rows and columns are a bit difficult to understand.
But the other day i saw that there are several (free and commercial)
gui builders which support formlayout.

For me personal i still like the old paper drawing and typing. Though
i wish i could place comments in the rows and column definitions,
because after a month of 2 it's difficult to understand without
comments.


Jaap
 
 
A. Bolmarcich





PostPosted: 2004-11-12 7:08:00 Top

java-programmer >> Newbie Question: Layouts On 2004-11-11, Debaser <email***@***.com> wrote:
> I've been having the toughest time with this....
> I want to have a JFrame with 3 containers in it.
> The containers are laid out top to bottom.
> Inside the first 2 containers I want 4 rows of a JLabel and
> JTextField. Components are lined up in both containers so that the
> labels end and textfields start at the same place on each line.
[snip]

Trying to get the horizontal position where the labels end and
textfields start to be the same in two different containers is
going to be difficult, if that horizontal position depends on
the preferred widths of the labels or textfields. Layout
managers are usually not designed to exchange layout information
with each other.

It would be better to use

- a single container that has all the rows, or

- a container that has all the label and a separate container
that has all the textfields

It is possible to use GridBagLayout and extra code that

- invokes getLayoutDimensions on the layout managers of the two
containers to get the width that each layout manager used
for its label column

- if the widths of the label columns are not the same, sets
the columnWidths field of the GridBagLayout with the smaller
width to be the width used by the by the other GridBagLayout,
and invokes layoutContainer to layout again (so it uses the
value of columnWidths that was just set)

It would likely be a good idea to write your own layout manager
that more directly exchange layout information with another
instance of itself.
 
 
Karsten Lentzsch





PostPosted: 2004-11-13 3:10:00 Top

java-programmer >> Newbie Question: Layouts Debaser wrote:

> [...]
> which is just a JTextArea. My questions are: what type should these
> containers be? what type of layout should I use for them (and what
> should the parameters to the method be if any)?

You can nest panels (and their layouts), or you can
implement this with a single panel and layout.

Nesting makes the implementation easy to read,
but typically you cannot align content that is
located in different panels. This is because
only very few layout managers offer inter-panel
layout constraint to align content over multiple
panels.

A frequent source for poorly aligned designs
is the TitledBorder. The Mac Aqua Style Guide
provides a chapter where you can study how to
replace the titled borders with alternatives
that often lead to better design.

I provide two presentation regarding this issue,
"First Aid for Swing" and "Advanced Layout and
Panel Building", see http://www.jgoodies.com/articles/

Best regards,
Karsten Lentzsch
 
 
Thomas G. Marshall





PostPosted: 2004-11-16 8:30:00 Top

java-programmer >> Newbie Question: Layouts Andrew Thompson coughed up:
> On Thu, 11 Nov 2004 12:59:34 -0500, Debaser wrote:
>
>> I've been having the toughest time with this....
>> I want to have a JFrame with 3 containers in it.
>> The containers are laid out top to bottom.
>> Inside the first 2 containers I want 4 rows of a JLabel and
>> JTextField. Components are lined up in both containers so that the
>> labels end and textfields start at the same place on each line. These
>> two containers also have a titled borders, as does the third
>> container which is just a JTextArea. My questions are: what type
>> should these containers be? what type of layout should I use for
>> them (and what should the parameters to the method be if any)?
>
> One way to achieve the basic effect you are after is
> a nested layout. The outermost layout would be a
> BorderLayout, that would contain your JTextArea in SOUTH.
>
> The top two areas would be in JPanels with another layout
> in the main NORTH and CENTER areas. Each JPanel would have
> a layout and/or other panels to further divide it. As best I
> understand your description, a further BorderLayout with the
> JLabels in a GridLayout in WEST, while the JTextFields are in
> a GridLayout in EAST.


AFAICT, from what it seems that the OP is asking, this would not suit his
needs. This does not /guarantee/ that the labels and textfields from the
1st component line up with the labels and textfields of the 2nd.


...[rip]...


--
Everythinginlifeisrealative.Apingpongballseemssmalluntilsomeoneramsitupyourn
ose.