File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Minimum Size & GridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Minimum Size & GridBagLayout" Watch "Minimum Size & GridBagLayout" New topic
Author

Minimum Size & GridBagLayout

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I'm using GridBagLayout in a complex form and I simply want to have each 'cell' be exactly the same width, when the size of the window changes the width of each cell scales accordingly, but each cell always remains the same size as the other. This way I can simply place items by specifying the location and number of cells it takes and have it always scale accordingly.
My problem is simple, the size of my cells are being adjusted because JLabels want more space, it's causing problems. I'm guessing that it's the JLabels minimum size being set based upon the text, which in turn causes the cell(s) it is in to modify their sizes to accomodate it. I don't want this! I dont' care if the text in the label gets cut off, I want to width of each cell to stay the same for each cell REGARDLESS of whether or not the components contained in them think they need more room.
How do I accomplish this? I thought I could just override each components minimumSize but that doesn't seem to work! I tried using columnWeights and columnWidths in GridBagLayout but that didn't seem to have any affect either! Help!!!
Phill Dixon
Greenhorn

Joined: Apr 01, 2005
Posts: 3
Try this - it should scale the text to size of label

public int getMaxSize(Font f, String text, double maxwidth, double maxheight, Graphics context) {
int size = f.getSize();
if (size == 0) size = 1;
boolean hasgotsmaller = false;
while (size > 0) { //might want to put a limit here in case something goes wrong e.g. while (size > 0 && size < 600) {
Font test = f.deriveFont((float)size);
Rectangle2D bounds = context.getFontMetrics(test).getStringBounds(
text, context);
if (bounds.getWidth() > maxwidth || bounds.getHeight() > maxheight) {
size--;
hasgotsmaller = true;
} else if (hasgotsmaller){
break;
} else {
size++;
}
}
System.out.println(size);
return size;
}

Hope this helps you
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Ken I think the solution stays in using minimum and prefferedSizes for your components, and also specifying weights for the gridbaglayout cells.

--
:alex |.::the_mindstorm::.


blog - InfoQ.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Minimum Size & GridBagLayout