• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Minimum Size & GridBagLayout

 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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::.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic