Disabling node in JTree

Hi!
I have a program and I want to disable a specific node in the tree when the user presses a button. My nodes are derived from IconNode, a class that extends DefaultMutableTreeNode.
I�ve tried to redefine setEnabled(state) in IconNode, but without success.
How can I do this?
Thanks a bunch

Make your node to be able to hold Enabled/Disabled state. On your button click, make your node to change state. Create custom TreeCellRenderer which will draw a node according to its state - you can use setEnabled(boolean) for a Label. Remember to notify TreeModel after your node changed its state.
Denis Krukovsky
http://dotuseful.sourceforge.net/

Similar Messages

  • Disableing nodes in JTree

    Hi All,
    is it possible to disable the nodes in a JTree. i.e. Grey them out, so user is not able to select.
    thanks

    if you use DefaultMutableTreeNode as your node's class, than it's not possible (does not contain any parameter to provide this functionality),
    but you can make your own derivation of this class, when you do it yourself
    (an interesting design question: what would you expect the disabled note to do? if you have some user-assigned action, you could probably disable it quite easily, but if you want to prevent the disabled note to be scrolled-up/down, I'm afraid it would be a bit more difficult)

  • Help needed with finding node in JTree

    Hi,
    In my application i will be entering some string data in a text box for search criteria.
    When I click on search, I will get data from database which will be shown as a JTree.
    This Jtree will have some nodes in which, node with string entered in the search text box exists.
    I need to focus on this node and I have only a string to find this node.
    How can I do this?How can I find a node in JTree using string value?
    Is there any alternate option by which I can achive my need?
    Please suggest.
    Thanks.

    @OP: please assign your dukes.
    @Andre_Uhres: if you don't get rewarded in the next couple of days, please let me know here to get at least one duke for your effort.
    Bye.

  • Problem inserting new node into JTree with depthFirstEnumeration()

    Hello,
    I'm currently trying to use depthFirstEnumeration() to add and delete nodes for a simple JTree. I've written a method to handle this, but it keeps coming back with an exception saying that 'new child is an ancestor'.
    All I'm trying to do is add and delete nodes from a JTree using add and remove buttons within a swing program. When a user adds the new node the JTree needs to be updated with new labels in sequential order dependent upon a depthFirst traversal of the JTree.
    My current code for the add button is as follows,
    public void actionPerformed(ActionEvent event) 
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
            Enumeration e = rootNode.depthFirstEnumeration();
            if(event.getSource().equals(addButton))
                if (selectedNode != null)
                    while (e.hasMoreElements()) {
                    // add the new node as a child of a selected node at the end
                    DefaultMutableTreeNode newNode = (DefaultMutableTreeNode)e.nextElement();
                     // treeModel.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
                    //System.out.print(newNode.getUserObject() + "" + newNodeSuffix++);
                    String label = "Node"+i;
                    treeModel.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
                    i++;
                      //make the node visible by scrolling to it
                    TreeNode[] totalNodes = treeModel.getPathToRoot(newNode);
                    TreePath path = new TreePath(totalNodes);
                    tree.scrollPathToVisible(path);
                    //System.out.println();
            else if(event.getSource().equals(deleteButton))
                //remove the selected node, except the parent node
                removeSelectedNode();           
        } As you can see in the above code, I've tested the incrementing of the new nodes in a System.out.print() statement and they appear to be incrementing correctly. I just can't see how to correctly output the labels for the nodes.
    Any help and advice always appreciated.

    This is about the 5th posting on the same question. Here is the link to the last posting so you can see what has already been suggested so you don't waste your time making the same suggestion.
    http://forum.java.sun.com/thread.jspa?threadID=704980
    The OP doesn't seem to understand that nodes don't just rename themselves and that renderers will only display the text of the current node. If you want the text displayed to changed, then you need to change the text associated with each node, which means iterating through the entire tree to update each node with new text. Then again, maybe I don't understand the question, but the OP keeps posting the same question without any additional information.

  • Drag and Drop nodes in JTree

    Is there a way to drap and drop JTree nodes without resorting to hacks? http://www.javaworld.com/javatips/jw-javatip97.html outlines a hack to get drag and drop working but it is kinda old. I am wondering whether there is a now a way to do this in Swing. http://weblogs.java.net/blog/shan_man/archive/2006/01/first_class_dra.html outlines a seemingly promising way but when I tried it it still wouldn't let me drag JTree nodes. (btw, if you are going to try it, you must replace TransferHandler.TransferInfo with TransferHandler.TransferSupport. It has apprently been renamed.)

    I have implemented drag and drop of nodes in JTree they same way it is explained in JavaWorld link that you mentioned, I customized it for my application and it worked great.

  • Wordwrap nodes in jtree

    Anyone know how we can do wordwrap of nodes in jtree?
    Please help...
    Thanks.

    Any example on this? what html code should I use? The simplest possible, eg
    yourNode.setText("<html>" + yourNodeText + "</html>");Most JComponents can render this html string and produce a good result. But if you wanna get a little more complex and use styles and other web things, then do as said, (two posts above) and use a JEditorPane specifically for cell renderer
    ICE

  • How to set the default tree node in JTree?

    I want to set a default tree node in JTree when start the program,
    what is the method or the function to call?
    Thanks~

    If you by "default" mean "selected", then you can use one of the setSelectionXXX methods in the JTree API.
    If you by "default" mean something else, never mind this post :-)

  • Setting custom color for selected node in JTree?

    Hi,
    i want to set my own color for selected node in JTree. i don't want to set for all the nodes...how to set Color for Selected node using TreeCellRender class?
    Thanks
    Mani

    I assume you are not setting a custom tree cell renderer...
    javax.swing.JTree theTree = ...;
    java.awt.Color newBGColor = ...;
    ((javax.swing.tree.DefaultTreeCellRenderer)theTree.getCellRenderer())
        .setBackgroundSelectionColor(newColor);

  • Add node to JTree

    I have an already populated JTree. At Runtime I add one or more node to JTree, using something like
    parentNode.add(newNode)....
    If I call a repaint method, the new node isn't visible, I have to reload model using
    DefaultTreeModel dtm = ((DefaultTreeModel)myTree.getModel());
    TreeNode rootNode = (TreeNode) dtm.getRoot();
    dtm.reload(rootNode);
    Unfortunately this method cause the lost of focus on the currently selected node and the collapse of all JTree around its root node; If I change an existing node (for example changing its text or icon) I must reload the model too!!!
    Does exist an other more "soft" method to change the aspect of a JTree without reload the entire model?

    You must use or implement the
        public void valueForPathChanged(javax.swing.tree.TreePath path,java.lang.Object newNode) {
            Object[]    p = path.getPath();
            Object[]    pp = p;
            //insert node here
            int[] ci = new int[] {index};
            Object[] cc = new Object[] {node};
            fireTreeNodesChanged(this, pp, ci, cc);
            fireTreeStructureChanged(this, pp, ci, cc);
        }from the TreeModel. To fire the last two is essensial.
    I implemented a GP in JAVA so i had the same problem wehn i started.

  • InvocationService on storage disabled nodes

    Hi,
    Is it safe to assume that InvocationService tasks will not fire on cluster nodes that were started using '-Dtangosol.coherence.distributed.localstorage=false'? I've tried both 'execute' and 'query' methods on a cluster that contains both storage-enabled and storage-disabled nodes and the tasks only seem to fire on the storage-enabled nodes. The reason that I'm curious is because the InvocationService.getInfo().getServiceMembers() method returns all the cluster nodes (not just the storage-enabled nodes), and the InvocationService threads show up in the thread dumps of the storage-disabled nodes.
    Thanks,
    Jim

    Hi Rob,
    Thanks for your reply. This turned out to be an elusive problem on my end, complicated by the fact that Coherence was 'eating' an exception. One of the member fields in my invocation class was not serializable, but the InvocationService thread did not give any indication of this error. It wasn't until I put a try/catch around the InvocationService.execute method that I discovered the problem. The local node was the only storage-enabled node, so that explains why the invocation was not being executed on the storage-disabled nodes.
    This might be a good candidate for a bug fix in Coherence (to log some indication that an exception occurred). As is, a good programming tip is to ALWAYS put a try/catch around InvocationService.execute() and InvocationService.query().
    Jim

  • How to disable selection of root node in JTree

    Hi all! Thanks for taking a minute to read my post!
    I am writing a really basic JTree for showing a list of items.
    Here is some of the code:
    /** Create a basic tree **/
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Title");
    DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    JTree tree = new JTree(treeModel);
    /** Method to return the string of the current highlighted selection **/
    public String getSelectionString()
    DefaultMutableTreeNode node =
    tree.getSelectionPath().getLastPathComponent();
    return (String)node.getUserObject();
    I would like to disable selection of the root node of my JTree.
    Thus, if I make a call to getSelectionString() above, it would return null instead of the string that represents the root label.
    I have read the following forum on disabling various TreePaths and TreeNodes in a JTree:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=224691
    This forum suggests implementing the TreeSelectionListener interface and the TreeSelectionModel class and over-riding the addSelectedPath() methods. I tried this suggestion, and I was able to enable and disable the different children of the Jtree, but the root of the JTree would not disable.
    I suppose that I could just simply remove the visibility of the root node, but I really want to avoid that option if possible.
    Wait --- let me be clear ---- I want to disable the selection of the root node only - still allowing selection of all its children.
    Any suggestions?
    Am I missing something really simple here?
    Did I explain my problem clearly?
    Thanks in advance!
    jewels

    simply try this..
    in the
    public void valueChanged(javax.swing.event.TreeSelectionEvent event);
    method of TreeSelectionListener impelentation get the
    TreePath tp = event.getPath();
    from TreePath get the component and then remove the selection from the treePath if it is the node u were checking/root node in this case
    tree.getSelectionModel().removeSelectionPath(tp);
    Try out....

  • How to disable a particular node in JTree?

    Hi
    I am working with an application where in which I have a tree component and when a select a node and click a disable button , I want to disable the selected node(that is no further selection of that node possible until I enable it). I am using DefaultTreeCellRenderer for rendering nodes. I tried the following.
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
    JLabel label = (JLabel) renderer.getTreeCellRendererComponent(tree,selectedNode,true,true,false,row,true);
    label.setEnabled(false);
    But it doesn't work.
    Does any one know how to do this?
    Thank you.

    Its not going to work if you implement the TreeCEllRenderer, cos' all it does is paint your tree.
    What u need to implement is the treeSelectionListener interface,
    and override the addSelectedPath(path), addSelectedPath(paths[]) etc.
    You might want to create a list of all the nodes that the user selectes to diable, and then in these methods decide if the node is that then do not allow its selection.

  • Making a node in JTree non expandable

    How do I make a node in a JTree non expandable. The nodes in the tree are DefaultMutableTreeNode. I have a tree with root R. It has children ch1, ch2, ch3. Nodes ch1 etc also have X number of children. What I want is if I click (either double click on the node or single clicking on the '+') on either of the child nodes ch1, ch2 and ch3 to disable expansion i.e. stop the tree collapsing beyond those nodes. I have tried tree.setExpandsSelectedPaths(false) and others but it still doesnt work. Any help much appreciated. Thanx.

    Thanx Jeanette, that is exactly!! what I'm looking for. Much appreciated.

  • Disable expand/collapse JTree through double click

    Hi guys,
    In my Tree when you double click a node, a input dialog will pop up. And the node will be expanded or collapsed accordingly. How can I disable expand or collapse the node through double click. So that every time only the input dialog will popup and the tree stays.
    Thanks a lot!

    The JTree class have a method setToggleClickCount(int clickCount) which sets the number of mouse clicks before a node will expand or close. The default is two. You can try to set 0 or for examle 10 and double click will only dialog popup.
    P.S.For the future RTFM and view jdk source code

  • How to dynamically add Nodes to JTree?

    How to add nodes to a Jtree by getting the information into a specific file
    For example, in JList:
    DefaultListModel workgroups;
    workgroups = new DefaultListModel();
    List<String> workgroupsList = new ArrayList<String>();
    workgroupsList = ParserUtils.getWorkgroupList(ParserUtils.getConfigPath() + "\\.workgroup.properties");
    if (!workgroupsList.isEmpty()){
         for (String workgroup : workgroupsList){
              workgroups.addElement(workgroup.toString().trim());
    workgroupList = new JList(workgroups); //adds the list workgroupsThe output of this will be a JList displaying the list of workgroups per line. The list could be found in a file name "workgroup.properties"
    Question is, is it possible to adapt this same method in JTree. The information per line will serve as one node in the tree. For example, I have 3 workgroups in the list, there will also be 3 nodes to be found in the tree.
    Any suggestions?
    THanks.

    There's a huge JTree example in the Swing tutorial. It's a bit of a beast to use.

Maybe you are looking for

  • Client Delete and Logical System and CUA

    I'm getting ready to do some client clean up in prep for an upgrade from ECC 5 to ECC 6 soon. Currently we have a 3 system landscape: DV1 QA1 PR1 Clients on these systems are as follows: (not including 000, 001, 066) PR1:100 - the production client Q

  • Have compaq desk top, trying to hook up HD flat screen monitor, won't work?

    Haave Compaq, desk top, trying to hook up, HD, flat screen monitor, won't work

  • Need to transfer files from iBook G4 To New Macbook Pro 13" HELP!

    My iBook G4 has a firewire 400 port, whereas the Macbook Pro has an 800 port. I am about to order a cable so that I can transfer my files via migration assistant, but just want to double check here to see if it will work. (I am getting VERY confused

  • Backup MDC makes my fcp projects corrupt

    When my MDC went down (someone turnet it OFF!!!), the backup mdc was in control. Everything played along nicely, except for one major problem. All fcp projects saved to the san were corrupt. They looked ok, had the correct filetype and size was ok, b

  • Flash doesn't work in Firefox?

    I installed the Flash Player 11.3 update, and now everything that uses flash shows up as a black screen. It works in chrome and safari however. I tried some article and discussion solutions, but none of them worked. & I don't have a realplayer plugin