tags:

views:

598

answers:

1

I'm not sure if GridBagLayoutManager is the only layout manager that does this, but here is my problem. I have 4 controls layed out horizontally in a GridBagLayout. To keep things simple for this example, each control get's an equal 1/4 of the form, and each control resizes with the same ratio as the other controls. The four controls are JTextField's where each text field maps to a column in a record from a ResultSet. Additional controls on the form allow one to navigate through the records.

If I navigate from one record to the next, then the text fields update their text to show the new data. However, the text fields also get automatically resized in proportion to the amount of text they are showing... text fields with a large amount of data expand in size, and text fields with a smaller amount of data get squished. If I run through 10 records in a ResultSet, then the controls are always resizing themselves and it looks quite bizarre to say the least.

What I would like to do is prevent these controls from resizing, unless (and until) the underlying container gets resized. So if I resize the window, then I would like the controls to resize (according to the "weight x" variable in the Layout), but I don't want the controls to resize just because the amount of text they are showing becomes more or less.

Anybody have any ideas here?

A: 

For JTextField (as mentioned in the contents) call setColumns(int) to set a preferred size on the text.

For JComboBox, call setPrototypeDisplayValue(Object) which will cause that value to be rendered and the preferred size of the JComboBox will be set based on that value.

In general, you can call setPreferredSize(Dimension) on any component directly to get the same behavior. General if not set the value is calculated based on some defaults on the component. What is happening with JTextField, JComboBox, and most JTextComponent derivatives. is that the preferred size on those components is driven by values the user is capable of changing (the text values, the combobox selection). Whereas with most other component (JButton, JCheckBox, etc) the content size doesn't really change when the user acts on it. Setting the columns and rows and the prototype display value fixes the value used to calculate the preferred size.

shemnon