• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1760
  • Last Modified:

JTree How to prevent selection of Tree node

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.

Thanks
0
richard_fairall
Asked:
richard_fairall
  • 2
  • 2
  • 2
2 Solutions
 
objectsCommented:
Not sure if you can achieve that, will have a think.
0
 
objectsCommented:
You actually should be able to achieve that with a custom TreeSelectionModel.
0
 
zzynxSoftware engineerCommented:
>> 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)
                   theTree.setSelectionPath(evt.getOldLeadSelectionPath());

            }
        });


to get the selected node:

        TreeNode selected = (TreeNode) theTree.getSelectionPath().getLastPathComponent();
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
richard_fairallAuthor Commented:
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)   {
          treeSelectionModel.clearSelection();
          treeSelectionModel.setSelectionPath(e.getOldLeadSelectionPath());
      }
    }
  }
0
 
richard_fairallAuthor Commented:
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.

0
 
zzynxSoftware engineerCommented:
Thank you
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now