javaswingjtreelook-and-feeldefaultmutabletreenode

JTree changes lineStyle when changing node's name


TLDR:

Updating the nodes of a JTree changes the lineStyle (pictures below) and I don't know why this happens and how to prevent it from happening as my code doesn't update any porperties of the CellRenderer at the point where these changes happen.


I have a client-server application in Java where I send a data request to the server and request some data from the DB based on the client request using the server and send the results back to the client. The results are then being displayed in a JTree, which already works fine.

The JTree thereby has some static (like a header) and some dynamic nodes where the static nodes are only being updated with values (which means the overall outline is always being displayed) and the dynamic nodes might be appended as result of one request and removed for the result of the next one.

At the moment I only implemented the static nodes when I realized this problem: When I change the value that my node displays it also changes the lineStyle property to somewhat strange that should not even be possible to choose from.

Here is a picture of how the JTree looks before and after updating a node:

JTree default outline

And this is what happens when I update a node:

updated JTree

As you can see somehow the vertical line going from the "Inspection Lot Number" node that should go down all the way just disappears while the other lines stay where the should be.

I update the nodes using this code:

public void renameNode(DefaultMutableTreeNode node, String renamedNode) {
    node.setUserObject(renamedNode);
    model.nodeChanged(node);
}

Here the parameter "node" is the node that I rename and the String is the new value I set it to (at least that's my Understanding of what happens here) and model is accessed using this line in the constructor of my class: model = (DefaultTreeModel) tree.getModel();.

I don't know if this might be important for a solution but at the moment I don't really have a defined TreeModel as I initiate my JTree using the root node and afterwards append all nodes dynamically. However it appears to me as if it has rather something to do with the TreeCellRenderer. For the renderer I currently use this configuration:

renderer.setSize(tree.getParent().getMaximumSize());

renderer.setLeafIcon(null);
renderer.setOpenIcon(null);
renderer.setClosedIcon(null);
renderer.setDisabledIcon(null);

I also tried forcing the JTree to use angled lines (which it always should do by default) by using tree.putClientProperty() according to the Oracle tutorial on JTree.

At this point I'm quite clueless what could be the issue. The only thing that makes sense for me to be the reason for this happening might be the TreeCellRenderer however I might also be completely wrong here. Any help would be really appreciated.


Solution

  • After some troubleshooting I finally realized that everytime I populated my JTree contrary to what could be seen the model didn't reset to 0 child nodes of the root node but it just stacked the changed nodes as new nodes as childs of the root node (so I got 7, 14, 21, ... child nodes, +7 for every time I populated my JTree).

    The reason for that was that I didn't know I had to call model.reload() after calling ((DefaultMutableTreeNode) tree.getModel().getRoot()).removeAllChildren(); (which I need to do to remove the tree completely so that I have my data only listed once in the JTree).

    Now that the JTree is being cleared completely before setting it up again, adding the nodes works perfectly using the method renameNode() provided in my original post.

    Hopefully this will help others that might face similar problems in the future.