Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Question How to disabled a node.

    Hi All,

    I am trying to disabled one node using following code:

    child = new DefaultMutableTreeNode("Inventory");
    parent.add(child);

    child.setEnabled(false);

    but when i am compiling it i am getting the following error:

    Navigate1.java:348: cannot find symbol
    symbol : method setEnabled(boolean)
    location: class javax.swing.tree.DefaultMutableTreeNode
    child.setEnabled(false);


    Please Help...:confused:

    Thanks in advance...:
    Last edited by smartsubroto; 06-24-2008 at 10:49 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Default

    child = new DefaultMutableTreeNode("Inventory");
    parent.add(child);

    child.setEnabled(false);

    model = new DefaultTreeModel(parent);
    tree = new JTree(model);
    tree.getSelectionModel().setSelectionMode(
    TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    linksRenderer = new DefaultTreeCellRenderer();
    linksRenderer.setBackgroundNonSelectionColor(Color .lightGray);
    linksRenderer.setBackgroundSelectionColor(Color.WH ITE);
    tree.setCellRenderer(linksRenderer);
    JScrollPane treeView = new JScrollPane(tree);
    treeView.setPreferredSize(new Dimension(250, 350));

    panel.add(treeView);
    treeView.setBounds(10,40,300,430);
    tree.setBackground(Color.lightGray);


    in the above code

    "Parent" is the root node and the "Child" is leaf of that node in a JTree.

    and i want to disable the leaf node..

    Plz suggest something

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I think you can try something like this. On a click of the leaf get the leaf name. Compare it with the name of the leaf you want to disable. If it's true make setEnable(false)

    You have try something like this at the start of code.

    Java Code:
    child.setEnabled(false);
    What happened there, did you debug and see it?

  5. #5
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Generally we get "cannot find symbol" error to such java classes when the imports for it are not present, please do check that once.

    Or another probable reason would be because u r making it disabled before adding it to the tree. Try first adding it to the mutabletree and then making it disabled at the end.(i am not sure of it, its just my guess)
    To finish sooner, take your own time....
    Nivedithaaaa

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, that's why I'm worried about the mentioned line of code. Just after declaring set to disable.

  7. #7
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Hi,
    Can u(person who initiated this thread) post the whole code from end to end instead of a part of it.

    Before thay try the given suggestion,that may solve ur problem
    To finish sooner, take your own time....
    Nivedithaaaa

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    That's the best thing if you grab the correct issue from the code. In shorter period you can have the solution.

  9. #9
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Default

    i use the following code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreeSelectionModel;

    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.tree.TreeSelectionModel;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.ImageIcon;
    import java.net.URL;
    import java.io.IOException;
    import javax.swing.JEditorPane;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;

    public class TreeDemo
    {
    JFrame f1;
    JPanel p1;
    static JTree tree;
    DefaultTreeModel model;
    DefaultTreeCellRenderer linksRenderer;
    DefaultMutableTreeNode parent,child1,child2;
    public TreeDemo()
    {
    f1=new JFrame("Tree Demo");
    p1=new JPanel();
    f1.getContentPane().add(p1);

    parent = new DefaultMutableTreeNode("Parent");

    child1 = new DefaultMutableTreeNode("Child1");
    parent.add(child1);

    child2 = new DefaultMutableTreeNode("Child2");
    parent.add(child2);

    child2.setEnabled(false);

    model = new DefaultTreeModel(parent);
    tree = new JTree(model);
    tree.getSelectionModel().setSelectionMode(
    TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    linksRenderer = new DefaultTreeCellRenderer();
    linksRenderer.setBackgroundNonSelectionColor(Color .lightGray);
    linksRenderer.setBackgroundSelectionColor(Color.WH ITE);
    tree.setCellRenderer(linksRenderer);
    JScrollPane treeView = new JScrollPane(tree);
    treeView.setPreferredSize(new Dimension(250, 350));

    p1.add(treeView);
    treeView.setBounds(10,40,300,430);
    tree.setBackground(Color.lightGray);

    f1.setSize(400,400);
    f1.show();
    }
    public static void main(String args[])
    {
    new TreeDemo();
    }
    }


    and the error..

    D:>javac treedemo.java
    treedemo.java:43: cannot find symbol
    symbol : method setEnabled(boolean)
    location: class javax.swing.tree.DefaultMutableTreeNode
    child2.setEnabled(false);
    ^
    Note: treedemo.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    1 error


    Plz... Suggest..:confused:

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  11. #11
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Default

    No i just want to block one child leaf.

    and also if we can block any one child then there will no problem in blocking of whole node..
    Thanks & Regards
    Subroto Bhattacharjee:)

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  13. #13
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Default

    setAllowsChildren(boolean) is used to set, wheather the node is able to have children or not.

    This command is not disabling the leaf..

    I use this code as:

    child2.setAllowsChildren(false);

    but it does nothing in this case..
    Thanks & Regards
    Subroto Bhattacharjee:)

  14. #14
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Hi,
    Check this link, it has something similar like what u want.

    How to disable a certain treenode in a jtree (Swing / AWT / SWT / JFace forum at JavaRanch)
    To finish sooner, take your own time....
    Nivedithaaaa

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by smartsubroto View Post
    setAllowsChildren(boolean) is used to set, wheather the node is able to have children or not.

    This command is not disabling the leaf..

    I use this code as:

    child2.setAllowsChildren(false);

    but it does nothing in this case..
    If I got you correctly this is what you want to do, right?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.tree.*;
    
    public class TreeDemo {
    
        JFrame f1;
        JPanel p1;
        static JTree tree;
        DefaultTreeModel model;
        DefaultTreeCellRenderer linksRenderer;
        DefaultMutableTreeNode parent,child1,child2, child3;
    
        public TreeDemo() {
            f1 = new JFrame("Tree Demo");
            p1 = new JPanel();
            f1.getContentPane().add(p1);
    
            parent = new DefaultMutableTreeNode("Parent");
            child1 = new DefaultMutableTreeNode("Child1");
            parent.add(child1);
    
            child2 = new DefaultMutableTreeNode("Child2");
            parent.add(child2);
    
            child3 = new DefaultMutableTreeNode("child3");
            child2.add(child3);
    
            model = new DefaultTreeModel(parent);
            tree = new JTree(model);
            tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
            linksRenderer = new DefaultTreeCellRenderer();
            linksRenderer.setBackgroundNonSelectionColor(Color .lightGray);
            //linksRenderer.setBackgroundSelectionColor(Color.WH ITE);
            tree.setCellRenderer(linksRenderer);
            JScrollPane treeView = new JScrollPane(tree);
            treeView.setPreferredSize(new Dimension(250, 350));
    
            child2.setAllowsChildren(false); // Try changing the status 
            
            p1.add(treeView);
            treeView.setBounds(10,40,300,430);
            tree.setBackground(Color.lightGray);
    
            f1.setSize(400,400);
            f1.show();
        }
    
        public static void main(String args[]) {
            new TreeDemo();
        }
    }

  16. #16
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Default

    here
    chil2.setAllowsChildren(false);

    is not useful to disable a leaf.
    Thanks & Regards
    Subroto Bhattacharjee:)

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  18. #18
    Join Date
    May 2008
    Location
    India,Kolkatta
    Posts
    50
    Rep Power
    0

    Default

    I just want to disable that particular leaf.

    for EX:
    if there is buttton say..
    JButton b1=new JButton("I Am Disable");
    b1.setEnabled(false);

    in the above case button is there but it is disabled.
    I just want to implement the same thing in leaf.
    Thanks & Regards
    Subroto Bhattacharjee:)

  19. #19
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Hi,
    did u check the link i provided before?
    To finish sooner, take your own time....
    Nivedithaaaa

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by smartsubroto View Post
    I just want to disable that particular leaf.

    for EX:
    if there is buttton say..
    JButton b1=new JButton("I Am Disable");
    b1.setEnabled(false);

    in the above case button is there but it is disabled.
    I just want to implement the same thing in leaf.
    Ok, in that sense disable a leaf is, it's there and you can't workout with it's child, isn't it?

Page 1 of 2 12 LastLast

Similar Threads

  1. JTree Programmatic Node Expansion and Selection Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 01-16-2013, 08:23 AM
  2. Node selection in jtree
    By Preethi in forum AWT / Swing
    Replies: 4
    Last Post: 06-20-2008, 12:25 AM
  3. Replies: 2
    Last Post: 06-03-2008, 10:07 PM
  4. Replies: 7
    Last Post: 05-09-2008, 08:54 AM
  5. How to Transmit data from one node to another
    By swimberl in forum Networking
    Replies: 2
    Last Post: 01-04-2008, 09:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •