How to ensure that text is rendered to fit in tree

2019-08-15 06:34发布

问题:

JTree does not seem to automatically resize its nodes to accommodate fonts of size 20+. I've tried calling revalidate(), but that doesn't seem to help. The text in the nodes appears "squished" vertically so that the top and bottom of letters are clipped.

How can I use large fonts and ensure that they are displayed properly with a JTree?

回答1:

Some LAFs (e.g. WindowsLookAndFeel) set a fixed rowHeight which is rather arbitrary (like 16px). Be sure either to force the tree to dynamically compute the height for each node:

 tree.setRowHeight(-1);

or calculate the rowHeight based on the tree's font and set that as the fixed height.



标签: java swing jtree