Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to auto-adjust font size of multiple JLabel based on container size in a smooth way?
    primarykey
    data
    text
    <p>I need to resize the font of multiple JLabel based on the scaling factor used to resize the container. To do this, I am setting the font of each JLabel to null so that they take the font of the container. It works, but it also produces strange results. </p> <p>To be specific, the text seems to "lag" behind the container and sometimes it gets even truncated. I would like to avoid this behavior. Any idea how?</p> <p>Example code simulating the behavior:</p> <pre><code>import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.geom.AffineTransform; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class TextResize implements Runnable { public static void main(String[] args) { TextResize example = new TextResize(); SwingUtilities.invokeLater(example); } public void run() { JFrame frame = new JFrame("JLabel Text Resize"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(800, 400)); Container container = frame.getContentPane(); container.setLayout(new BorderLayout()); final JPanel labelContainer = new JPanel(new GridBagLayout()); labelContainer.setBorder(BorderFactory.createLineBorder(Color.black)); //initial font final Font textFont = new Font("Lucida Console", Font.PLAIN, 10).deriveFont(AffineTransform.getScaleInstance(1, 1)); labelContainer.setFont(textFont); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.insets = new Insets(0, 10, 0, 10); c.weightx = 1; for (int i = 0; i &lt; 5; i++) { JLabel f = new JLabel("Text here with possibly looooooooong words"); f.setBorder(BorderFactory.createLineBorder(Color.green)); f.setFont(null);//take the font from parent c.gridy = i; labelContainer.add(f, c); } JSlider slider = new JSlider(0,50000,10000); slider.addChangeListener(new ChangeListener() { double containerWidth = labelContainer.getPreferredSize().getWidth(); double containerHeight = labelContainer.getPreferredSize().getHeight(); @Override public void stateChanged(ChangeEvent ev) { JSlider source = (JSlider) ev.getSource(); double scale = (double) (source.getValue() / 10000d); //scaling the container labelContainer.setSize((int) (containerWidth * scale), (int) (containerHeight * scale)); //adjusting the font: why does it 'lag' ? why the truncation at times? Font newFont = textFont.deriveFont(AffineTransform.getScaleInstance(scale, scale)); labelContainer.setFont(newFont); //print (font.getSize() does not change?) System.out.println(scale + " " + newFont.getTransform() + newFont.getSize2D()); } }); container.add(slider, BorderLayout.NORTH); JPanel test = new JPanel(); test.setLayout(null); labelContainer.setBounds(5, 5, labelContainer.getPreferredSize().width, labelContainer.getPreferredSize().height); test.add(labelContainer); container.add(test, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } } </code></pre> <p>Picture: <a href="http://i.stack.imgur.com/tZLOO.png" rel="nofollow">http://i.stack.imgur.com/tZLOO.png</a></p> <p>Thanks,</p> <p>-s</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload