Prevent node selection when expanding node in JTree

I created JTree , and some of its nodes are customizable to show them as extensible, although they have no children (for now). I executed this thread to implement this.

Why? I want to load the tree dynamically, so when the tree expands, I retrieve additional information from the server and display it on the tree.

The problem I am facing is that when I expand one of these nodes, it becomes selected, which is not the default behavior for the default nodes (you can expand them without changing the tree selection).

How to solve this problem to prevent selection of this node when expanding?

+3
source share
1 answer

I do not understand this problem with the code below. You must do something else that we cannot guess without seeing your code. Try to take the code below and modify it to reproduce the problem you are seeing. By doing this, there is a big chance that you will find what you do differently and why you get this behavior (see also SSCCE ).

The code was taken from here

 import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeNode; public class DynamicTree extends JFrame { public class OutlineNode extends DefaultMutableTreeNode { private boolean areChildrenDefined = false; private int outlineNum; private int numChildren; public OutlineNode(int outlineNum, int numChildren) { this.outlineNum = outlineNum; this.numChildren = numChildren; } @Override public boolean isLeaf() { return false; } @Override public int getChildCount() { if (!areChildrenDefined) { defineChildNodes(); } return super.getChildCount(); } private void defineChildNodes() { // You must set the flag before defining children if you // use "add" for the new children. Otherwise you get an infinite // recursive loop, since add results in a call to getChildCount. // However, you could use "insert" in such a case. areChildrenDefined = true; for (int i = 0; i < numChildren; i++) { add(new OutlineNode(i + 1, numChildren)); } } @Override public String toString() { TreeNode parent = getParent(); if (parent == null) { return String.valueOf(outlineNum); } else { return parent.toString() + "." + outlineNum; } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new DynamicTree(5); } }); } public DynamicTree(int n) { super("Creating a Dynamic JTree"); setDefaultCloseOperation(EXIT_ON_CLOSE); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { // TODO Auto-generated catch block e.printStackTrace(); } Container content = getContentPane(); JTree tree = new JTree(new OutlineNode(1, n)); content.add(new JScrollPane(tree), BorderLayout.CENTER); setSize(300, 475); setVisible(true); } } 
+4
source

All Articles