Results 1 to 9 of 9
  1. #1
    mishathegoat is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Question Netbeans: Add nodes to JTree?

    Hello everyone,
    I'm kinda stuck. I am trying to add nodes to my JTree during runtime.. but I can't seem to get it to work. I have jTree1 in a jFrame, "testFrame.java" made with Netbeans GUI designer. I just want to add simple text like, "Hello World" to jTree1. What am I doing wrong? How can I accomplish this task?

    I appreciate your help thanks :-)


    -JavaApplication1
    ----Source Packages
    -------javaapplication1
    ------------Main.java
    ------------testFrame.java


    testFrame.java
    Java Code:
    package javaapplication1;
    
    public class testframe extends javax.swing.JFrame {
    
    [COLOR="Red"][B][SIZE="4"]public static DefaultMutableTreeNode helloWorldText = new DefaultMutableTreeNode("HelloWorld");[/SIZE][/B][/COLOR]
    
        public testframe() {
            initComponents();   
        }
        
        @SuppressWarnings("unchecked")
    
        private void initComponents() {
    
     [SIZE="1"]       jScrollPane1 = new javax.swing.JScrollPane();
            jTree1 = new javax.swing.JTree();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jScrollPane1.setViewportView(jTree1);
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
    [/SIZE]    }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new testframe().setVisible(true);  
                }
            });
        [COLOR="Red"][B][SIZE="4"]jTree1.add(helloWorldText); 
    [COLOR="DarkGreen"][B]//Produces error: 
    //non-static variable jTree1 cannot be referenced from a static context
    //
    //cannot find symbol
    //symbol: method add(javax.swing.tree.DefaultMutableTreeNode)
    //location: class javax.swing.JTree[/B][/COLOR]
    [/SIZE][/B][/COLOR]
        }
    
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTree jTree1;
    
    }

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    jTree1 is a non static member of testFrame, but you are using it in the static method main.

    if you move it to the end of the init components method (probably just before the pack()), it should run correctly.

  3. #3
    mishathegoat is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for your help this is very frustrating.

    Okay. So Netbeans will not allow me to modify any of that auto-generated code (Where that init method is).

    So I made my own method, addNodes().. And was about to add code to the addNodes() method but I didn't matter.

    The line that has:
    Java Code:
    jTree1.add(helloWorldText);
    Now the only error it produces is:
    Java Code:
    cannot find symbol
    symbol: method add(javax.swing.tree.DefaultMutableTreeNode)
    location: class javax.swing.JTree
    Again, thanks for your help.. Do you have any other ideas?

    Thank you


    The newly modified code of testFrame.java:
    Java Code:
    package javaapplication1;
    
    import javax.swing.tree.*;
    
    public class testframe extends javax.swing.JFrame {
    
        [COLOR="Red"][B]public static DefaultMutableTreeNode helloWorldText = new DefaultMutableTreeNode("HelloWorld");[/B][/COLOR]
    
        public testframe() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            jTree1 = new javax.swing.JTree();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jScrollPane1.setViewportView(jTree1);
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
     [COLOR="Red"]   public void addNodes() {
            [SIZE="4"][B][COLOR="Red"]jTree1.add(helloWorldText); //////THIS LINE PRODUCES THE ERROR\\\\\\[/COLOR][/B][/SIZE]
        }[/COLOR]
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new testframe().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTree jTree1;
        // End of variables declaration
    }

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

    Default

    Before that, where you define this?

    Java Code:
    org.jdesktop.layout

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    1. Read the API for JTree and follow the link to the Swing tutorial on How to Use Trees where you will find working sample codes.
    2. Until you understand what the code does and how to work with the IDE, ditch the visual designer. Beleieve me, when you're new to Swing it's actually easier to hand-code a GUI than it is to discover the IDE features that allow you to tweak it to your needs.

    db

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

    Default

    I agreed with Darryl. As a newbie to Java it's much better to avoid this kind of IDEs and code in a simple editor like Notepad from the scratch.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    I wouldn't recommend using Notepad for Swing programming. The formatting, autocomplete and Fix imports features of NetBeans are a real boon.

    The pain comes when trying to tweak a layout designed in Matisse, or simply when adding functionality. While there's almost nothing that can be done by hand coding that can't be done in NetBeans/Matisse, it takes greater in-depth understanding of Swing coding to be able to achieve the result in a visual designer. And that kind of knowledge can only be garnered by a few years of hand-coding GUIs.

    db

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

    Default

    But I'm suggesting to use Notepad at the beginning for Java newbies. But yes, no need to stick with it for a long time. I hate UI designers, they never let us to learn new things in our way.

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

Similar Threads

  1. How to dynamically add Nodes to JTree?
    By javanewbie in forum AWT / Swing
    Replies: 3
    Last Post: 07-13-2009, 07:20 AM
  2. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  3. JcheckBoxes as JTree Nodes
    By aneesahamedaa in forum AWT / Swing
    Replies: 11
    Last Post: 02-11-2009, 12:11 AM
  4. nodes
    By Dr Gonzo in forum New To Java
    Replies: 1
    Last Post: 12-08-2008, 04:22 PM
  5. Nodes displayed in JTree
    By Orange in forum AWT / Swing
    Replies: 6
    Last Post: 08-08-2008, 05:07 AM

Tags for this Thread

Posting Permissions

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