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


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

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

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 2016-12-08 15:30 -0600
Organization A noiseless patient Spider
Message-ID <o2cjas$2ag$1@dont-email.me> (permalink)
References <75624c6e-6a78-4e8a-82a4-4ff2556cc241@googlegroups.com> <f5777763-3658-4b03-b122-711f15545d7f@googlegroups.com>

Show all headers | View raw


On 12/8/2016 14:48, The Kreepy Gamer wrote:
> If this is what you meant, I have added more code so it can be more easily compiled.
>
> public static void main(String[] args) {
>         JFrame window = new JFrame();
>         JLabel label = new JLabel("Hey! That's pretty good!");
>         window.add(label);
>         window.setSize(300,300);
>         label.setFont(getMaxFontSize(label,label.getText()));
>         window.setVisible(true);
> }
>
> 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);
> }
>

That can't be compiled and that's one reason you aren't getting any 
answers.  See http://mindprod.com/jgloss/sscce.html.

Since your code above won't compile as it is, I'm not sure if the fact 
that you appear to be creating your GUI on a thread other than the Event 
Dispatch Thread (EDT) is the problem.  Swing components must be created 
and most component methods must be called from the EDT.  Not doing so 
can cause unending problems with your program.

So give us an SSCCE and I'm sure that you will get an answer to your 
problem.

-- 

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