highlight the treeNode in the Jtree using the treeNode itself


I have a JTree (<strong>myTree</strong>) and in another class, I have a DefaultMutableTreeNode which was taken from <strong>myTree</strong>.

In a certain function, I want the JTree to highlight the node.

<em><strong>I tried:</strong></em>

myTree.setSelectionPath(new TreePath(treeNode));

but visually nothing is happening.

any ideas?


I have also another JTable which is rendered based on the selected treeNode in <strong>myTree</strong>. The table is updating <strong>correctly</strong>. It's just the <strong>myTree</strong> which refused to update visually.


You need to use the actual tree path of the node. Not just an instance of TreePath:

myTree.setSelectionPath(new TreePath(treeModel.getPathToRoot(treeNode)));

Also, the javadoc says:


If any component of the path is hidden (under a collapsed node), and getExpandsSelectedPaths is true it is exposed (made viewable)


So make sure that getExpandsSelectedPaths is true.


From the java API, we get the description of TreePath below:


Represents a path to a node. A TreePath is an array of Objects that are vended from a TreeModel. <strong>The elements of the array are ordered such that the root is always the first element (index 0) of the array</strong>.


so, a valid TreePath must be constructed from an array including all nodes on the path from the root node and the node you want to select.


Many Swing bugs, quirks and other shortcomings can be worked around using <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeLater%28java.lang.Runnable%29" rel="nofollow">SwingUtilities.invokeLater(Runnable)</a>:

<pre class="lang-java prettyprint-override">SwingUtilities.invokeLater(new Runnable() { @Override public void run() { // JB Nizet's solution: myTree.setSelectionPath(new TreePath(treeModel.getPathToRoot(treeNode))); } });

This also solves the same problem with <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JTree.html#setSelectionPaths%28javax.swing.tree.TreePath%5b%5d%29" rel="nofollow">JTree.setSelectionPaths(TreePath[])</a>.


  • Docker nginx reverse proxy gives “502 Bad Gateway”
  • Overriding vals in Scala
  • WCF hosting issues
  • What is the official release update URL for Aptana Studio 3.0?
  • Update SQL MS Access 2010
  • Is it safe to drop the -webkit vendor prefix from the css3 border-radius yet?
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • Changing references to deprecated methods C++
  • Primefaces lazy datascroller calling load twice
  • Configure nginx to return different files to different authenticated users with the same URI
  • uml Composition relationships to RDF and OWL
  • Salesforce Different WSDL files and when to use
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • ASP.NET MVC Application won't update some controllers
  • Multicolored edittext hint
  • Redux Form - Not able to type anything in input
  • Copy to all folders batch file?
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • How can I sort a a table with VBA with given text condition?
  • Time complexity of a program which involves multiple variables
  • D3 nodes and links from JSON with nested arrays of children
  • Checking free space on FTP server
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • Delete MySQLi record without showing the id in the URL
  • Calling of Constructors in a Java
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Unanticipated behavior
  • Comma separated Values
  • retrieve vertices with no linked edge in arangodb
  • PHP: When would you need the self:: keyword?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Trying to get generic when generic is not available
  • Understanding cpu registers
  • Add sale price programmatically to product variations