How to force an HTML JLabel in a JTree to resize when the font changes

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-03-12T03:11:59Z Indexed on 2010/03/12 3:17 UTC
Read the original article Hit count: 385

Filed under:
|
|

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.

Any help would be greatly appreciated. Thanks in advance!

-Mike

© Stack Overflow or respective owner

Related posts about java

Related posts about swing