File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes unable to get alignment and widths right Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "unable to get alignment and widths right" Watch "unable to get alignment and widths right" New topic
Author

unable to get alignment and widths right

Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
i have attached below an applet i am trying to create. I am a beginner with Swing so please explain me if you find any problems below.

this applet just features a normal form.
the problems i have identified are:
1. i have labels for fields tag & description. They are not getting aligned to left
2. The text field and text area fields are not getting displayed properly.
3. i want the submit button to expland to the entire grid width, in my case it is 2.

i have tried to do the above but does not work? can any one tell me why? Thanks for your help.

Applet Code:


Raghavan
SCJP 6
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
Hello Craig,

Thanks for your reply and revised code.

I am not sure whether you checked the display of the applet as it is not displayed properly. The problems are
1. text area does not expand
2. text field does not expand properly in width

I have also a few questions for you
1. why did you prefer to put the code in init() as I have put it in start(). Do you think it makes it a bit faster to load?

2. Why have you used a scroll pane for text field and not used for a text area?

3.
Originally posted by Craig Wood:


i understand grid width specifies the number of cells a component can occupy. i wanted tagLabel and descriptionLabel to span one cell each and wanted Tag and Description to span two grid cells each. I also understood that weightx and weighty specifies how much of space relatively a component should acquire when its container is stretched. What did you actually meant in youur comments?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I am not sure whether you checked the display of the applet as it is not displayed
properly. The problems are
1. text area does not expand
2. text field does not expand properly in width

Yes I checked the display repeatedly as I made one change at a time in an attempt to get
your layout to behave as you described in the beginning.
Item 2 was: 2. The text field and text area fields are not getting displayed properly.
I wasn't sure what you meant by properly. So I wrapped the JTextField in a
JScrollPane so it would show up. Having the textField columns set to 200 caused it (and
the textArea) to collapse (GridBagLayout was unable to display it at this size in the
available space). If you want them both to expand it can be done. So much rides on the
small details of what you are trying to achieve in your layout that it's difficult to do
too much given sketchy or possibly–incomplete information.
1. why did you prefer to put the code in init() as I have put it in start(). Do you
think it makes it a bit faster to load?

The init method is the proper method to use for gui construction. I don't know that
either one is faster. We override the start method for animations in which we start
up threads/timers.
2. Why have you used a scroll pane for text field
Just to get it to show up; it was too large to be displayed at 200 columns. Try changing
the columns argument to a smaller value like 20 and it shows up okay.
... and not used for a text area?
I didn't know what you were trying to do with the textArea.
i understand grid width specifies the number of cells a component can occupy. i wanted
tagLabel and descriptionLabel to span one cell each and wanted Tag and Description to span
two grid cells each.

The gridwidth does specify how many columns a component is to occupy. However, in a two–
column layout, specifying a gridwidth of 2 for a component in the right hand column will
not cause the display area of the component to be increased. In your code it appears that
you were trying to increase the relative horizontal space occupied by the text components
by adding an "extra column" with the gridwidth constraint set to a value of 2. This won't
work. The number of columns is determined only by the number of components in the row
which contains the maximum number of components in the layout.
By "span two grid cells each" you might be indicating that you want these components to
occupy more space in the layout. We do this by judicious (by way of experiment) use of the
weightx constraint.
I also understood that weightx and weighty specifies how much of space relatively a
component should acquire when its container is stretched. What did you actually meant in
youur comments?

The weight constraints determine how much of the extra/available space in the layout a
component may occupy. If a weight constraint is zero a component will be displayed at its
preferredSize in the respective dimension (x, y). If a weight constraint in non–zero the
components display area (in the respective dimension) may increase beyond its
preferredSize. This is one way to create fluid layouts.
For example, let's say you want the labels to occupy minimal horizontal space and the text
components to occupy the rest of the available horizontal space. Further, let's specify
that these components will be clumped together in the center of the vertical space (ie,
weighty = 0). And since you are creating a form let's align the labels to the right of
their diplay areas. And add the new information that the text components should fill the
horizontal space (I don't know about the textArea filling the vertical space). Given these
specs you could do something like this.

Note: While modifying the code from my last post I noticed that I had forgotten to
comment-out/remove this line:

This was an oversight.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to get alignment and widths right