tags:

views:

210

answers:

1

I'm updating a Java Swing application to support the user switching the app's font from normal size to a larger size (so the user can switch between the two sizes at runtime). One problem I'm having is with a JTree that uses HTML for the tree nodes to underline the text in some nodes (the HTML is just embedded in the JLabel of each tree node). One extra thing to know about the nodes is that they're a custom component, adding a JCheckBox in front of each JLabel.

The problem is that once the JTree is visible, increasing the font size causes the nodes (containing underlined text) to not resize. The HTML for those nodes seems to prevent the node from becoming wider, so when the font changes, the text becomes truncated.

I think my options are to either: 1) use another approach to make the text underlined, since removing the HTML from the JLabel causes it to resize properly when the font size changes, or 2) keep the HTML formatting and somehow force the JTree/JLabels to resize when the font size is updated (possibly by firing a property change event?).

The code already calls SwingUtilities.updateComponentTreeUI() on the parent JFrame when the font size gets updated.

EDIT: The method used to change the font in the application is explained here.

Any help would be greatly appreciated. Thanks in advance!

-Mike

+1  A: 

I cannot recreate the problem you describe. Here is a test program that works for me on JavaSE 6:

public class JTreeFontResize {
    private static JTree tree;
    private static JFrame frame;

    public static void main(String[] args) throws InterruptedException,
            InvocationTargetException {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                tree = new JTree(new Object[] { "One (plain)",
                        "<html><u>Two (HTML)", "<html>Three (HTML)" });
                frame = new JFrame("Tree Font Resize");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(100, 100, 300, 300);
                frame.add(tree);
                frame.setVisible(true);
            }
        });

        Thread.sleep(2000);
        changeFontSize(20);

        Thread.sleep(2000);
        changeFontSize(30);

        Thread.sleep(2000);
        changeFontSize(12);
    }

    private static void changeFontSize(final int size) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Font font = new Font("Vernanda", Font.PLAIN, size);
                FontUIResource fontResource = new FontUIResource(font);
                Enumeration<Object> keys = UIManager.getDefaults().keys();
                while (keys.hasMoreElements()) {
                    Object key = keys.nextElement();
                    Object value = UIManager.get(key);
                    if (value instanceof FontUIResource) {
                        UIManager.put(key, fontResource);
                    }
                }
                SwingUtilities.updateComponentTreeUI(frame);
            }
        });
    }
}

If the above works for you then maybe you should post a cut down version of your problematic code.

Russ Hayward
This is the right answer. After some additional testing based on the above, the problem lies with the custom component used to add a checkbox to each tree node. Thanks for the help!
Mike