• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unable to get alignment and widths right

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Raghavan Chockalingam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic