JButton - Set font to fill  
Author Message
SPG





PostPosted: 2005-7-10 21:37:00 Top

java-programmer, JButton - Set font to fill Hi,

I want to create the best fitting font for my jbutton, so when the user
expands the UI, the JButton expands or shrinks, so I then change the size of
the font to fill the button correctly..

I am trying something like this on component resize:


 
SPG





PostPosted: 2005-7-10 21:40:00 Top

java-programmer >> JButton - Set font to fill
"SPG" <email***@***.com> wrote in message
news:dw9Ae.66682$email***@***.com...
> Hi,
>
> I want to create the best fitting font for my jbutton, so when the user
> expands the UI, the JButton expands or shrinks, so I then change the size
> of the font to fill the button correctly..
>
> I am trying something like this on component resize:
>
>
Sorry... inadvertently sent before completion!!

Here is the code I am using:

private void handleFontRescale(){
JButton b = _buttons[0];
Dimension d = b.getSize();
Font f = b.getFont();
int size = 1;
Graphics g = b.getGraphics();
Font rescaledFont = f;
while(true){
Font newFont = new Font(f.getName(), f.getStyle(), size);
if( g.getFontMetrics(newFont).stringWidth(" ") >= d.width * 2 /
3
|| g.getFontMetrics(newFont).getHeight() >= d.height * .5){
for( int i=0; i < _buttons.length; i++){
_buttons[i].setFont(rescaledFont);
}
break;
}
else{
size++;
rescaledFont = newFont;
}
}
}

As you can see, I am setting the size to 1, then increasing it till I hit
the required size o fthe button.
The problem is that I am still getting "..." when I start shrinking the
fonts (on screen resize smaller), so I guess my calculations are incorrect
(IE: how much of the button dimension can I take up in text?

Can anyone point me in the right direction?

Steve


 
usenet





PostPosted: 2005-7-13 3:05:00 Top

java-programmer >> JButton - Set font to fill SPG <email***@***.com> wrote:
>
> "SPG" <email***@***.com> wrote in message
> news:dw9Ae.66682$email***@***.com...
>> Hi,
>>
>> I want to create the best fitting font for my jbutton, so when the user
>> expands the UI, the JButton expands or shrinks, so I then change the size
>> of the font to fill the button correctly..
>>
>> I am trying something like this on component resize:
>>
>>
> Sorry... inadvertently sent before completion!!
>
> Here is the code I am using:
>
> private void handleFontRescale(){
> JButton b = _buttons[0];
> Dimension d = b.getSize();
> Font f = b.getFont();
> int size = 1;
> Graphics g = b.getGraphics();
> Font rescaledFont = f;
> while(true){
> Font newFont = new Font(f.getName(), f.getStyle(), size);
> if( g.getFontMetrics(newFont).stringWidth(" ") >= d.width * 2 /
> 3
> || g.getFontMetrics(newFont).getHeight() >= d.height * .5){
> for( int i=0; i < _buttons.length; i++){
> _buttons[i].setFont(rescaledFont);
> }
> break;
> }
> else{
> size++;
> rescaledFont = newFont;
> }
> }
> }
>
> As you can see, I am setting the size to 1, then increasing it till I hit
> the required size o fthe button.
> The problem is that I am still getting "..." when I start shrinking the
> fonts (on screen resize smaller), so I guess my calculations are incorrect
> (IE: how much of the button dimension can I take up in text?
>
> Can anyone point me in the right direction?

Just use getPreferredSize() instead of the low-level Font calculations.



Christian