PDA

View Full Version : Auto Sizing Dialogs



Clay Ferguson
1 Sep 2009, 10:42 AM
Here is the best way I could come up with to automatically size my dialogs so that they all come up initially sized to exactly fit (just surround) their content:




public abstract class MyDialog extends Window {

private LayoutContainer content;
private boolean sizeSet = false;

public MyDialog() {
setLayout(new BorderLayout());

content = getDlgContentPanel();

BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
centerData.setMargins(margins);
add(content, centerData);
setSize(1, 1); //size is unknown as of now
show();
}


@Override
protected void onAfterLayout() {
super.onAfterLayout();

if (!sizeSet && getInnerHeight() > 0) {
sizeSet = true;
setSize(content.getWidth()+margins.left+margins.right+getFrameWidth(),
content.getHeight()+margins.top+margins.bottom+getFrameHeight());
center();
}
}

public abstract LayoutContainer getDlgContentPanel();
}



Anyone know of a better way of doing this? Please don't tell me to hard code the sizes of all my dialogs. That would be rediculous because, depending on the font sizes and style settings, things will be different in size if any of that changes. The whole purpose of layouts is to avoide those types of dependencies. So this means dialogs are sized differently than most other things. Normally you can have the inside compoents flow to fit for most situations, but dialogs are the opposite... dialogs need to be 'sized' to just barely fit what is inside the dialog... in most cases.