Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GridBagLayout gridwidth, fill and anchor  RSS feed

 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Having a bit of a problem with positioning, if anyone can help at all please!

I've added a JTextArea to my panel, but it comes out tiny by default. So I set the gridwidth to 3 (to align with fields above) and the fill to BOTH. Which obviously hasn't worked. Anchoring also (while not appropriate in this instance) gets ignored, i guess because of the fill value.

Is there anything in particular I'm missing? Maybe minimum sizing, or insets? Maybe getting the bounds for the TextField above? I know swing isn't ideal for form views, but thought it was a good way to learn it!

image:


code:


where the arguments are component, inset, gridx, gridy, gridwidth, gridheight, weightx, weighty, fill, anchor
The JTextFields have a length of 10

Thanks,

Nick
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess the problem is in how you define the JTextArea. I'm guessing your are using something like:



Resulting in the preferred width of the text area being greater than the width of the two text fields and JLabel.

So I see two approaches:

1. set the second text area to fill its space
2. make the width of the text area smaller (since you have set it to fill the width when necessary).

If you need more help (and for future questions) post a proper SSCCE that demonstrates the problem so we don't have to guess what you may or may not be doing.
 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob, thanks for the quick reply.

I'm actually just using new JTextArea() as the constructor, and using the fill value to give it width. I'd use 'new JTextArea(*width of field1 + label2 + field2*, height) if I could

I had just given up and changed the vehicle textArea to a textField, but I'm having a pretty similar problem aligning a 'submit' button below.

The problem seems to be that I can't right align anything because I don't know how to return the width of the textfields.

It's a shame there doesn't seem to be a way to measure the width of the grids themselves, that might help (ie gridwidth - textfield width)

Nick

* edit: it seems literally impossible to nicely line up a submit button, on the right or left, because the text on each label doesn't start at the beginning of the grid, and the fields don't finish on the right (they end after 10 'columns')

I think perhaps you are on to something by setting the field width to fill as well
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use 'new JTextArea(*width of field1 + label2 + field2*, height) if I could


No, that is not what I am suggesting.


When you do:



the text field will calculate its preferred width based on support 10 characters of the current Font size.

So by using new JTextArea(row, columns), you specify the row/column, not the actual pixel value and the text area will determine its preferred size.

So if you use 20, then its preferred size should be less than the 2 text fields and JLabel.

I'm actually just using new JTextArea()


Then the width should not be an issue. Since it is greater than the text field, then my next guess is that you are using frame.setSize(...) instead of frame.pack(). When specifying the size the text area will grow to fill the space. When using pack the text area should only grow to be right aligned with the text field, since the text fields will control the preferred width.

Without a proper SSCCE, I can't offer any more advice, in this or future postings. As I said above, I don't have time to guess what you may or may not be doing.
 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for not providing all the code, don't mean to waste your time!

yes, the customer details panel is contained in a frame that uses .setSize().

i'll definitely look in to .pack().

i'm going to have a mess about with it now (and am making a much more simplified version for ease of use) - if I still struggle would you mind having a look at an SSCCE if you get the time?

Nick
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!