• Post Reply Bookmark Topic Watch Topic
  • New Topic

Layout clarification

 
Vrinda Werdel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am need to develop a UI where I need to capture data. So it will be a combination of Labels and TextFields on each line. For instance,


First Name -----------------
Last Name -----------------
Street -----------------

which layout can be used to best represent this.

regards

Vrinda
 
Mark Van Tuyl
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a GridLayout on your JFrame with 3 rows.
Each of these rows could contain a JPanel which uses a FlowLayout.
Each JPanel would contain one JLabel and one JTextField.

If you would like some example code, please let me know.
 
Vrinda Werdel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark. Will try this approach.

regards
Vrinda
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use gridbag layout. It has a little bit of a learning curve, but if you want to be particular about how things line up it seems to be the best bet.
Unless you want to try a third party layout (like forms layout from jgoodies) which I've not used myself.

-Tad
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to guarantee they line up vertically then I recommend a GridBagLayout as Tad suggested. Here's a few hints to help you out with that. First, set the GridBagConstraints.fill to GridBagConstraints.NONE if you don't want your components to take up the entire cell. Second, use GridBagConstraints.anchor to set where they're positioned within the cell when the cell is bigger than the component. Third, use GridBagConstraints.weightx to specify the weighting to be given to it when assigning extra space.

So, in your example, to make vertical row of fields that look like this:



The setup would look something like this:



Obviously the variables ending in "Label" are the JLabel's and the variables ending in "Field" are the JTextField or whatever you're using.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!