Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2005

Tip: Looking for answers? Try searching our database.

Help with GridBagLayout

Thread view: 
- - 21 Aug 2005 13:42 GMT
Using GridBagLayout, the width of a JComboBox expands when it is set to
editable.

I have the following layout (where b is the JComboBox).

+------------------------------------+
|[      a        ] [  b  ] [   c    ]|
|                                    |
|                                    |
+------------------------------------+

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.VERTICAL;

gbc.gridx = 0;
gbc.gridy = 0;
panel.add(createA(), gbc);

gbc.fill = GridBagConstraints.VERTICAL;
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(createB(), gbc);

gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 2;
gbc.gridy = 0;
panel.add(createC(), gbc);

When the combo box is set to editable the row expands outside the dialog
that contains the panel.

Other than setting the preferred size of B, is there another way?
Knute Johnson - 21 Aug 2005 18:55 GMT
> Using GridBagLayout, the width of a JComboBox expands when it is set to
> editable.
[quoted text clipped - 29 lines]
>
> Other than setting the preferred size of B, is there another way?

-:

The simplest solution is to call pack() on your main panel after
changing the size of the JComboBox.  These are always tricky issues.
The other option is make your panel big enough to accomodate the largest
JComboBox possible.  The problem with that is you never really know for
sure how big that is supposed to be.

knute...
email s/nospam/knute/
Larry Barowski - 21 Aug 2005 21:13 GMT
> Using GridBagLayout, the width of a JComboBox expands when it is set to
> editable.
[quoted text clipped - 3 lines]
>
> Other than setting the preferred size of B, is there another way?

Subclass JComboBox to create a combo box that returns
the widest of super.getPreferredSize() for the editable and
non-editable sizes. Something like this:

private boolean forceEditable;
private boolean forceEditableState;

public Dimension getPreferredSize()
{
  forceEditable = true;
  forceEditableState = true;
  Dimension editableSize = super.getPreferredSize();
  forceEditableState = false;
  Dimension nonEditableSize = super.getPrererredSize();
  forceEditable = false;
  ...
}

public boolean isEditable()
{
  if(forceEditable)
     return forceEditableState;
  return super.isEditable();
}

This is sort of hacky, but it works on all Java versions
as far as I know. We do a similar thing to make the
preferred size of the editable combo box equal to that
of the uneditable one, so it is wide enough to show
any item in the list.
HGA03630@nifty.ne.jp - 21 Aug 2005 23:33 GMT
Is Larry's code correct?
I'd like to see the full code of his getPreferredSize() method.
Larry Barowski - 22 Aug 2005 02:55 GMT
> Is Larry's code correct?
> I'd like to see the full code of his getPreferredSize() method.

I thought that was obvious.

if(isEditable()) {
  if(nonEditableSize.width > editableSize.width)
     editableSize.width = nonEditableSize.width;
  return editableSize; }
if(editableSize.width > nonEditableSize.width)
  nonEditableSize.width = editableSize.width;
reutrn nonEditableSize;

Another option is to just compute and return the
non-editable size. If you always have some items
in the list with a significant width, that might be
what you want.
Thomas Hawtin - 22 Aug 2005 00:58 GMT
> Using GridBagLayout, the width of a JComboBox expands when it is set to
> editable.
[quoted text clipped - 5 lines]
>
> Other than setting the preferred size of B, is there another way?

The way I'd (initially) go about it is:

 o Create a shadow copy of the combo box.
 o Set the copy to enabled but non-visible.
 o Add the combo box to a panel with BorderLayout (unlike
GridBagLayout, BorderLayout ignores isVisible).
 o Set the panel to non-opaque (which it might already be).
 o Add with same constraints as the real combo box.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

- - 22 Aug 2005 02:54 GMT
> Using GridBagLayout, the width of a JComboBox expands when it is set to
> editable.
[quoted text clipped - 29 lines]
>
> Other than setting the preferred size of B, is there another way?

I noticed that when the display area is smaller than the total size of
the components, the "[  b  ]"  becomes "[     b     ]" (increase in
size) but when i make the display area bigger, it gives the desired
appearance.

Is there a way I can ensure it remains the same when the display area is
smaller?
Larry Barowski - 22 Aug 2005 12:14 GMT
> I noticed that when the display area is smaller than the total size of the
> components, the "[  b  ]"  becomes "[     b     ]" (increase in size) but
> when i make the display area bigger, it gives the desired appearance.
>
> Is there a way I can ensure it remains the same when the display area is
> smaller?

Once all the elements can not fit using the preferred
widths, GridBagLayout will switch to using the
minimum widths. This can cause a much different
layout, and some items to actually get larger, as you
have seen. I tend not to use GridBagLayout in situations
where the size of the component can go below the
preferred size. Putting the component in a scrollpane
may be an option, or you can tinker with the minimum
sizes of the components to get the effect you want.
Setting the minimum width of the combo box to zero
would be a place to start.

I think a more sensible behavior for GridBagLayout
would be to give each item an equal fraction of its
difference between minimum and preferred size
when the preferred size can not be achieved (in
other words, use that difference as the weight when
between minimum and preferred size). That would
provide a smooth transition between the can/can't
reach preferred size states, with behavior equivalent
to the current GridBagLayout at the preferred and
minimum size boundaries.
Thomas Hawtin - 22 Aug 2005 13:15 GMT
> I think a more sensible behavior for GridBagLayout
> would be to give each item an equal fraction of its
[quoted text clipped - 6 lines]
> to the current GridBagLayout at the preferred and
> minimum size boundaries.

I believe that's what now happens in mustang from b43.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4238932

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Larry Barowski - 22 Aug 2005 18:53 GMT
>> I think a more sensible behavior for GridBagLayout
>> would be to give each item an equal fraction of its
[quoted text clipped - 10 lines]
>
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4238932

Surprising, since it could break some layouts that were
designed to work a certain way given the old behavior.
I considered submitting this as an RFE years ago, but
never even checked to see if it was there, as I assumed
they would never change it.
Babu Kalakrishnan - 22 Aug 2005 20:36 GMT
>>>I think a more sensible behavior for GridBagLayout
>>>would be to give each item an equal fraction of its
[quoted text clipped - 16 lines]
> never even checked to see if it was there, as I assumed
> they would never change it.

I found it surprising as well - but for different reasons - Having
ignored this for a long 6 years - wonder how Sun engineers finally
realized that there was a problem and that the fix was trivial..

The GridBagLayout was one whose implementation made the whole world
world rdicule even the concept of LayoutManagers. The old implementation
deserves a place in Java's hall of shame.

BK


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.