I need to change the font size of several JLabels based on the scaling factor used to resize the container. To do this, I set the font of each JLabel to null to take the font of the container. It works, but it also gives strange results.
To be specific, the text appears to be โlagging behindโ the container, and sometimes it even becomes truncated. I would like to avoid this behavior. Any idea how?
Example code that simulates behavior:
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));
Photo: http://i.stack.imgur.com/tZLOO.png
Thank,
-s
java fonts resize swing jlabel
fusio Mar 21 '12 at 23:59 2012-03-21 23:59
source share