Improved JTree node rendering and font

I have a problem with render nodes in JTree. When you change the font of node and node, the text becomes wider, and the text of the node is truncated, and the end of the text is replaced by dots. How to tell JTree that it should expand the scope for rendering the entire node.

thanks for the help

+4
source share
4 answers

You can use your own renderer and set something like this for the component (JLabel)

final Dimension size = label.getPreferredSize(); label.setMinimumSize(size); label.setPreferredSize(size); 

or just set the text as follows

 setText("<html>" +valueText+"</html>") 
+3
source

It seems that the font change trigger happens under the feet of the tree: internally, the ui delegate does a lot of size caching, which should be updated with any changes that affect the cached sizes. This is done automatically when you make changes to treeModel, the corresponding state of the extension, and some visual changes in the tree itself.

So the main question is: what causes the font to change? If this is some change in the model / nodes, the implementation of the model is incorrect, without launching the corresponding TreeModelEvent, the obvious solution would be to fix it :-) If this is something outside the model, the solution depends on the details of your context, nothing common applies.

0
source

Cache size

JTree uses renderer to render nodes. renderer is the same renderer for all operating systems, so the distinguishing elements are inside ComponentUIs . JTree uses JLabel by default to draw nodes, so its JLabel wo size allows us to cut text using ...

Let's take a short action: Swing has different LookAndFeels for different Operating Systems, they are separated from components in UI classes, for example BasicLabelUI (and this is the source of your problem). BasicLabelUI caches the label size to prevent recounting if no changes have been made. So, BasicLabelUI did not clear the cache of old size values. BasicLabelUI clear the cache if it finds out about any changes.

The question is why BasicLabelUI did not receive change information? Well, if you change / expand / rename the Tree programmatically, you should say that ComponentUI should delete this cache!

You are in luck, you do not need to write a lot of code, because the genius has already written something for you, the creators of the TreeUI class Rob Davis and Scott Violet wrote startEditingAtPath and stopEditing.

Example

 TreeUI ui = tree.getUI(); for (TreePath treePath : selectionPaths) { ui.startEditingAtPath(tree, treePath); } tree.setSelectionPaths(selectionPaths); tree.expandPath(expandPaths.getSelectionPath()); ui.stopEditing(layer); 
0
source

Call TreeModel reload()

-1
source

Source: https://habr.com/ru/post/1339145/


All Articles