Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2005

Tip: Looking for answers? Try searching our database.

Grid layout

Thread view: 
miki.miska@email.si - 21 Sep 2005 18:31 GMT
Hi !

I am having trouble with putting simple GUI on my desktop.

I want to have something like this.

+-----------------------------+
|   Customer ID: XXXXXXXX     |
|     Reference: XXXXX        |
|           Bla: XXXXXXXXXX   |
+-----------------------------+

The whole stuff must be centered within the panel, while
text on the left side is right justified (all : are in same column), and
data is left justified.

If I use Gridlayout, everything is in the center, and it look very bad.
I am adding stuff into the pane in this order:

add(new JLabel("Customer ID:"))
add(new JLabel("XXXXXXX"))
add(new JLabel("Reference:"))
add(new JLabel("XXXXX"))
add(new JLabel("Bla:"))
add(new JLabel("XXXXXXXXXXX"))

Can someone give me IDEAD which Layout manager would be the simplest one
to do this ?

Thx.
Oliver Wong - 21 Sep 2005 18:55 GMT
> I want to have something like this.
>
[quoted text clipped - 3 lines]
> |           Bla: XXXXXXXXXX   |
> +-----------------------------+

[snip]

> Can someone give me IDEAD which Layout manager would be the simplest one
> to do this ?

   See "A Visual Guide to Layout Managers":
http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html

   In particular, the last example
(http://java.sun.com/docs/books/tutorial/figures/uiswing/layout/SpringForm.gif)
looks like exactly what you want.

   - Oliver
Thomas Hawtin - 21 Sep 2005 21:30 GMT
>>+-----------------------------+
>>|   Customer ID: XXXXXXXX     |
>>|     Reference: XXXXX        |
>>|           Bla: XXXXXXXXXX   |
>>+-----------------------------+

>>Can someone give me IDEAD which Layout manager would be the simplest one
>>to do this ?

>     In particular, the last example
> (http://java.sun.com/docs/books/tutorial/figures/uiswing/layout/SpringForm.gif)
> looks like exactly what you want.

I wouldn't wish SpringLayout on my worst enemy.

If GridLayout wont do what's wanted, although it may suffice in this
case, then GridBagLayout will do most things. It's something that you
should get yourself acquainted with.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Oliver Wong - 21 Sep 2005 21:51 GMT
>>>+-----------------------------+
>>>|   Customer ID: XXXXXXXX     |
[quoted text clipped - 14 lines]
> then GridBagLayout will do most things. It's something that you should get
> yourself acquainted with.

   I retrospect, I suspect that you are right in accusing me of not giving
the correct answer, given that the question was "Which layout manager would
be the SIMPLEST one to do this?" As recommended by Michael Dunn in another
thread, the problem could have been solved with the GridLayout.

   That being said, while I admit SpringLayout is complicated, I certainly
think its complexity is within tolerable ranges. I'm using SpringLayout in
an application I'm developing right now actually. And no GUI builder
either - it's being coded by hand.

   - Oliver
Thomas Hawtin - 21 Sep 2005 22:36 GMT
>     That being said, while I admit SpringLayout is complicated, I certainly
> think its complexity is within tolerable ranges. I'm using SpringLayout in
> an application I'm developing right now actually. And no GUI builder
> either - it's being coded by hand.

What's your secret? It's been a little while (Jan '04) since I played
with it, but I found it completely unpredictable. Certainly you need to
look at the source because the documentation is incomplete. The tutorial
doesn't go for real world situations and doesn't do a good job of the
more realistic examples.

(My ramblings from when I played with SpringLayout:
http://jroller.com/page/tackline/20040107#cushioning_springlayout )

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Oliver Wong - 22 Sep 2005 16:06 GMT
>>     That being said, while I admit SpringLayout is complicated, I
>> certainly think its complexity is within tolerable ranges. I'm using
[quoted text clipped - 6 lines]
> go for real world situations and doesn't do a good job of the more
> realistic examples.

   Really? I found the tutorial gave me exactly the information I needed.
That, with the addition of the SpringUtilities class at
http://java.sun.com/docs/books/tutorial/uiswing/layout/example-1dot4/SpringUtili
ties.java

let me handle every situation that I came across. Maybe I've just been
working with particularly simple GUIs so far.

   My main reason for not using GridLayout was that I didn't want all the
columns to have the same width. Maybe I'll take a closer look at
GridBagLayout next time.

   - Oliver
Roedy Green - 22 Sep 2005 00:24 GMT
>    See "A Visual Guide to Layout Managers":
>http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html

A have added sample screen shots to the various layout manager
descriptions at http://mindprod.com/jgloss/layout.html

That may help narrow down candidates faster.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Michael Dunn - 21 Sep 2005 20:27 GMT
> Hi !
>
[quoted text clipped - 21 lines]
> add(new JLabel("Bla:"))
> add(new JLabel("XXXXXXXXXXX"))

perhaps aligning the label is all you need to do

import java.awt.*;
import javax.swing.*;
class Testing extends JFrame
{
 public Testing()
 {
   setLocation(400,100);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   JPanel p = new JPanel(new GridLayout(3,2));
   p.add(new JLabel("Customer ID:",JLabel.RIGHT));
   p.add(new JLabel("XXXXXXX"));
   p.add(new JLabel("Reference:",JLabel.RIGHT));
   p.add(new JLabel("XXXXX"));
   p.add(new JLabel("Bla:",JLabel.RIGHT));
   p.add(new JLabel("XXXXXXXXXXX"));
   getContentPane().add(p);
   pack();
 }
 public static void main (String[] args){new Testing().setVisible(true);}
}
Roedy Green - 21 Sep 2005 23:09 GMT
>Can someone give me IDEAD which Layout manager would be the simplest one
>to do this ?

There may be simpler ways of doing it, but GridBagLayout will do it
since you can adjust the alignment of each component individually.

You use both the alignment ability of JLabel and the GridBagLayout.

Using insets you can control the padding around your JLabels.

I normally do the right hand side with JTextFields with
setEditable(false). That makes it clearer which is data and which is
label.

see http://mindprod.com/jgloss/gridbaglayout.html
http://mindprod.com/jgloss/layout.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Karsten Lentzsch - 21 Sep 2005 23:30 GMT
> [...]
> Can someone give me IDEAD which Layout manager would be the simplest one
> to do this ?

Often grid-based layout managers do a well job.
There are free third party layout managers that
can implement more layouts than the GridBagLayout,
and some of them are easier to understand and to use.

I provide a presentation about layout issue, see
"Layout and Panel Building" at my articles page
http://www.jgoodies.com/articles/
I discuss problems, goals, concepts and present
a reference implementation for a grid-based layout
system, the JGoodies Forms. See also the
"JGoodies Forms" whitepaper for a discussion
of several Java layout managers.

Scott Violet has presented an overview of Java
layout system at this year's JavaOne. He categorizes
and compares a layout concepts, layout managers,
and layout systems. I don't have the URL at hand
for the JavaOne 2005 slides, but they are free.

Hope this helps. Kind regards,
Karsten Lentzsch


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.