tags:

views:

56

answers:

2

In our product, we have frames that are basically three inheritance levels down from what is essentially a JDialog. This frame overrides the default pack() method as shown:

@Override
public void pack() {
    this.setSize(getMaximumSize());
    validate();
    super.pack();
}

@Override
public Dimension getMaximumSize(){
    return super.getPreferredSize();
}

pack() here gets called after pretty much everything is on the screen. Lots of these windows have a title bar, a couple of tool bars, a pane that holds the main content (can often be mostly empty), and a status bar at the bottom.

My problem appears to be that when it calls the getPreferredSize() for the container, the result is just too small, i.e. the width seems okay, but the height isn't. Reading through the docs, it seems like the preferred size is computed based on the layout manager if it isn't set explicitly (which I'm pretty sure it isn't). I'm not quite sure how it calculates or if I should be doing something else first.

Anybody have any idea or thoughts as to what my problem could be here? It's not always too small, just some of the time. Please let me know what other information/code may be helpful to figuring this out. Thanks.

A: 

I believe most of the Layouts will use the basic premise of getting the preferred size of the immediate children of the container and then returning the minimum Dimension that would contain those components laid out using their preferredSizes (a quick check of the source shows this is the case for FlowLayout, BoxLayout, and GridLayout). So two suggestions I can offer are:

  1. Read the source for getPreferredLayoutSize(Container) for the Layouts you use (or the ones you use most frequently) and confirm they are using preferred sizes for their calculations.
  2. Set a preferredSize explicitly for the lowest level components (items that aren't a composite of other components), then the composites of those components should have an appropriate preferredSize in relation.
M. Jessup
A: 

It looks like there is stuff being added to the dialog after the pack() and the getPreferredSize() happen, however those additions aren't resizing things properly.

As is often the case, the answer turned out to be rather pedestrian and obvious.

Morinar