JTree How to prevent selection of Tree node

Posted on 2005-04-25
Last Modified: 2007-12-19
I have a JTree and I dont want to disable nodes.
In certain circumstances I would like to perform the following:

When the user clicks on a particular node (other than the currently selected one),
I want to prevent selection of that node and pop up a warning message.
(The warning message is not the issue)
The tree node selection must not change to the clicked one. The original
selected node must remain selected.
Presumably, I could reselect the originally selected node after closing the popup,
but it wouldn't look so good.

The selection process appears to take place before any mouse events are caught
by the listener.

Question by:richard_fairall
    LVL 92

    Expert Comment

    Not sure if you can achieve that, will have a think.
    LVL 92

    Assisted Solution

    You actually should be able to achieve that with a custom TreeSelectionModel.
    LVL 37

    Accepted Solution

    >> The original selected node must remain selected.

            theTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
                public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {

                    if (the currently selected node shouldn't be selected)


    to get the selected node:

            TreeNode selected = (TreeNode) theTree.getSelectionPath().getLastPathComponent();

    Author Comment

    Thanks objects,

      For some reason I have to add the clearSelection call or it floods with events, but it works fine.
      I check both old and new selection paths are valid (presumably means a change in node selection)

      public void valueChanged(TreeSelectionEvent e)
        if (e.getSource() != null)   {
          if (e.getOldLeadSelectionPath() != null && e.getNewLeadSelectionPath() != null)   {

    Author Comment

    Sorry, so used to objects replying I didn't notice it was zzynx who
    responded last. I had already got a custom TreeSelectionModel in situ
    before I read the last answer, so I'll split the points.
    Thanks both.

    LVL 37

    Expert Comment

    Thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Title # Comments Views Activity
    Exchange and Third Party application. 8 45
    what is spring 3 36
    wordsWithout 49 64
    count7 challenge 12 52
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now