How to make the tree have a loading icon


How to make the tree have a loading icon, such as this:,I try to pass
this.myTree.setCellRenderer(new MyRenderer());
Call the setIcon method in MyRenderer to set the icon, but the icon is static and cannot be moved, although the icon I set is dynamic

public class MyRenderer extends MouseAdapter implements TreeCellRenderer {
private static final Icon LOADING_NODE_ICON = JBUIScale.scaleIcon(EmptyIcon.create(8, 16));

private final NodeRenderer nodeRenderer;
private final PresentationData presentation;

protected NodeRenderer createRenderer() {
return new NodeRenderer() {
protected @Nullable ItemPresentation getPresentation(Object node) {
return ReadAction.compute(() -> getPresentationInner(node));

ItemPresentation getPresentationInner(Object node) {
if ((node instanceof DatabaseStructure.DbRootGroup)) {
return ((DatabaseStructure.DbRootGroup) node).getPresentation();
if ((node instanceof DataSource)) {

return ((DataSource) node).getPresentation();
if (node instanceof DataSourceTreeNodeDescriptor) {
return ((DataSourceTreeNodeDescriptor) node).getElement().getPresentation();
} else if (node instanceof DefaultMutableTreeNode) {
Object userObject = ((DefaultMutableTreeNode) node).getUserObject();
if (userObject instanceof DataSourceTreeNodeDescriptor) {
return ((DataSourceTreeNodeDescriptor) userObject).getElement().getPresentation();
return null;

public MyRenderer() {
this.nodeRenderer = this.createRenderer();
this.presentation = new PresentationData();

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
SimpleColoredComponent component = (SimpleColoredComponent) this.nodeRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
component.setIcon(new AnimatedIcon.Default());
return component;

If I use tree.setPaintBusy(true) directly, there will be 2 problems:
The first one: the position of the icon is wrong
The second: I need to set loading for a certain node instead of all


Please see as reference. It uses to obtain current test node’s icon and also uses new AnimatedIcon.Default() as general “loading” icon.


Please sign in to leave a comment.