Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36064

Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this?

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Knute Johnson <nospam@rabbitbrush.frazmtn.com>
Newsgroups comp.lang.java.programmer
Subject Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this?
Date Wed, 7 Dec 2016 13:36:37 -0600
Organization A noiseless patient Spider
Lines 37
Message-ID <o29o9l$3qo$1@dont-email.me> (permalink)
References <75624c6e-6a78-4e8a-82a4-4ff2556cc241@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Wed, 7 Dec 2016 19:35:17 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="f7ad8df9e9a5d5c1b3fc79b5321f5708"; logging-data="3928"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0C3qgiCQLFBRwdOd2iMRK"
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1
In-Reply-To <75624c6e-6a78-4e8a-82a4-4ff2556cc241@googlegroups.com>
Cancel-Lock sha1:cPTywyU3b3AZzL94JvXCLWmRQvA=
Xref csiph.com comp.lang.java.programmer:36064

Show key headers only | View raw


On 12/6/2016 17:21, The Kreepy Gamer wrote:
> In my GUI program, I have a thread with a while loop that resizes the text to fit into the various JLabels and JButtons that are on it. But sometimes when the text is a certain width it will get bigger and smaller. For example, it will have text in the font size 26, it loops and resizes it to 20. Then it loops again and changes it back to 26, creating a "jiggling" effect.
>
> Here is what I have:
>
> public static Font getMaxFontSize(Component comp, String text) {
> 	Font compFont = comp.getFont();
> 	String compText = text;
> 	int stringWidth = questionLabel.getFontMetrics(compFont).stringWidth(compText);
> 	int compWidth = comp.getWidth()-75;
> 	
> 	if (stringWidth == 0) {
> 		stringWidth++;
> 	}
> 	double widthRatio = (double)compWidth / (double)stringWidth;
> 	
> 	int newFontSize = (int)(compFont.getSize() * widthRatio);
> 	int compHeight = comp.getHeight();
> 	
> 	int fontSizeToUse = Math.min(newFontSize, compHeight);
> 	
> 	if (fontSizeToUse < 1) {
> 		return new Font(compFont.getName(), Font.PLAIN, 1);
> 	}
> 	return new Font(compFont.getName(), Font.PLAIN, fontSizeToUse);
> }
>
> Does anyone know why it is doing this, and how I could fix it?
>
> Thanks!
>

An SSCCE might get you some answers.

-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? The Kreepy Gamer <tylerc2077@gmail.com> - 2016-12-06 15:21 -0800
  Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-07 13:36 -0600
  Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? The Kreepy Gamer <tylerc2077@gmail.com> - 2016-12-08 12:48 -0800
    Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-12-08 15:30 -0600
    Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-08 22:17 +0000
      Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-08 20:09 -0500
      Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Gene Wirchenko <genew@telus.net> - 2016-12-09 09:07 -0800
        Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-09 20:01 +0000
  Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Nigel Wade <nmw@local.domain> - 2016-12-09 10:54 +0000
  Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? The Kreepy Gamer <tylerc2077@gmail.com> - 2016-12-09 11:35 -0800
    Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-09 15:32 -0500
    Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-09 21:22 +0000
      Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-12 12:08 +0000
    Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-11 20:48 -0500
  Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? The Kreepy Gamer <tylerc2077@gmail.com> - 2016-12-12 11:18 -0800
    Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 14:22 -0500
    Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-12 20:15 -0500

csiph-web