Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade Newsgroups: comp.lang.java.programmer Subject: Re: When resizing text to fill a JLabel the text sometimes "jiggles", how do I fix this? Date: Fri, 9 Dec 2016 10:54:12 +0000 Lines: 83 Message-ID: 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 X-Trace: individual.net zupofgKRuJfF1laVBD/lGA4qC4eJ+qiKzwnMBLK9QHTRgVZfQ1 Cancel-Lock: sha1:p91oqbgwDk68x2ne3q2W2mgTZP8= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: <75624c6e-6a78-4e8a-82a4-4ff2556cc241@googlegroups.com> Xref: csiph.com comp.lang.java.programmer:36074 On 06/12/16 23: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? Presumably, somewhere in the code you haven't shown us, you set the font of comp to the new font, and then sometime later do the same again. It's bound to oscillate because you calculate the new font size pretty much entirely based on the current font size. It's up to you to determine which part of your code logic is broken. We have no idea what the intent of the code is supposed to be. To see it in action (some unusable aspects of your method reworked to make it executable): package fonts; import java.awt.Component; import java.awt.Font; import javax.swing.JTextField; public class Fonts { static Font aFont = new Font("default", Font.PLAIN, 20); static int fontSize = aFont.getSize(); static JTextField component = new JTextField(""); public static void main(String[] args) { Font f = aFont; component.setFont(f); for (int i = 0; i < 10; i++) { f = getMaxFontSize(component, "a test string"); component.setFont(f); System.out.println(f); } } public static Font getMaxFontSize(Component comp, String text) { Font compFont = comp.getFont(); String compText = text; int stringWidth = comp.getFontMetrics(compFont).stringWidth(compText); int compWidth = 140; if (stringWidth == 0) { stringWidth++; } double widthRatio = (double) compWidth / (double) stringWidth; int newFontSize = (int) (aFont.getSize() * widthRatio); int compHeight = 30; int fontSizeToUse = Math.min(newFontSize, compHeight); if (fontSizeToUse < 1) { return new Font(aFont.getName(), Font.PLAIN, 1); } return new Font(aFont.getName(), Font.PLAIN, fontSizeToUse); } }