Results 1 to 7 of 7
  1. #1
    Orange is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default Nodes displayed in JTree

    Hi, i'm creating a JTree with TreeModel, and make the root as my c: drive. when the tree displayed, i found that it display some files that are actually not available in my c: drive. so, can anyone please tell me what's wrong in this case? thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,320
    Rep Power
    25

    Default

    Sounds like a program logic error. Check your program. If you need help, you'll have to post the program so others can look at it.

  3. #3
    Orange is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Sounds like a program logic error. Check your program. If you need help, you'll have to post the program so others can look at it.
    yup, i really need help. the following is my code, please check for me. thanks in advance!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*; 
     
    public class MyTree extends JFrame implements ActionListener{
    	
    File[] rt;
    File drive;
    private DefaultComboBoxModel comboModel;
    MyTreeModel model;
    File root = null;
    JTree tree;
       
     
        public MyTree() {
        	 
            initComponents();
    				
        }
        
        
    public void initComponents() {
    	
    setLayout(new BorderLayout());
      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
      	
    panelB = new javax.swing.JPanel();
    panelB.setLayout(new BorderLayout());
      
    comboModel = new DefaultComboBoxModel();
    selectDrive = new JComboBox(comboModel);
      
     rt=File.listRoots();
      
           
      for (int i=0; i<rt.length; i++)
      {
      	drive = rt[i];
      	comboModel.addElement(drive);
      }
      selectDrive.setModel(comboModel);
      
      selectDrive.addActionListener(this);
      selectDrive.setActionCommand("sel_drive");
      tree = new JTree();
      tscrollPane = new JScrollPane(tree);
      displayTree();
    	
      panelB.add(selectDrive,BorderLayout.NORTH);
      panelB.add(tscrollPane,BorderLayout.CENTER);
     
      add(panelB,BorderLayout.NORTH);
    	
      pack();
    	
    }
     
     
      public void actionPerformed(ActionEvent e) {
        	String cmd = e.getActionCommand();
    	
    	    if(cmd.equals("sel_drive")){
    		  	displayTree();	
    		  }
      }
     
     public void displayTree(){
     	Object obj = comboModel.getSelectedItem();
     	String str = obj.toString();
     	File f = new File(str);
     	
     	model = new MyTreeModel(f);
     	tree.setModel(model);
     }
     
     public static void main(String args[]){
     	MyTree treefr = new MyTree();
     	treefr.setSize(500, 500);
     	treefr.setVisible(true);
     	
     }
     
        public  JComboBox selectDrive;
        public  JPanel panelB;
        public  JScrollPane tscrollPane;
    }
    Java Code:
    import javax.swing.tree.*;
    import javax.swing.event.*;
    import java.io.*;
     
    class MyTreeModel implements TreeModel {
      
      public File root;
      
      public MyTreeModel(File rt) { 
      	root = rt; 
      }
     
      
      public Object getRoot() { 
      
      	return root; 
      }
     
      
      public boolean isLeaf(Object node) {  
      	
      	File leaf = (File)node;
      	return leaf.isFile(); 	
      }
     
      
      public int getChildCount(Object parent) {
      	File p = (File)parent;
      	if(p.isDirectory()){ 
      		System.out.println("Total Files="+p.list().length);
      		return p.list().length;
      	}else{ 
      		return 0;
      	}
        
      }
     
      
      public Object getChild(Object parent, int index) {
      	
        File p = (File)parent;
        String[] children = p.list();
        File child = new File(p,children[index]);
            
            try
            {
            	return child;
            }
            catch (ArrayIndexOutOfBoundsException e)
            {
                e.printStackTrace();
                return null;
            }
      }
     
      
      public int getIndexOfChild(Object parent, Object child) {
        String cname;
        String[] children = ((File)parent).list();
        
        if (children.length>0){
        	
        	 cname = ((File)child).getName();
        	 for(int i = 0; i < children.length; i++) {
          	if (cname.equals(children[i])) 
          	{	
          		return i;
          	}
          }
        }
        else{
        	
        		return -1;
        }
        return -1;
      }
     
      
      public void valueForPathChanged(TreePath path, Object newvalue) {}
      public void addTreeModelListener(TreeModelListener l) {}
      public void removeTreeModelListener(TreeModelListener l) {}
      
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,320
    Rep Power
    25

    Default

    YOu need to fix your program. The first thing I get is an error message:
    ava.lang.Error: Do not use MyTree.setLayout() use MyTree.getContentPane().setLayout() instead
    at javax.swing.JFrame.createRootPaneException(Unknown Source)
    at javax.swing.JFrame.setLayout(Unknown Source)
    at MyTree.initComponents(MyTree.java:27)
    at MyTree.<init>(MyTree.java:20)
    at MyTree.main(MyTree.java:80)

  5. #5
    Orange is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    YOu need to fix your program. The first thing I get is an error message:
    but these two java files run well in my jdk1.6.0_01 without any compilation and
    run time error? :confused:

    is this the problem of different jdk?

    actually, i tried to get some JTree sample code from internet and run in my pc without any modification, i get the same problem too, the tree display some files and folders that actually not existed in my pc...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,320
    Rep Power
    25

    Default

    some files that are actually not available in my c: drive
    How do you know those files do NOT exist? What program are you using to view the files? Perhaps the files are hidden by the OS. Try looking at the contents of a drive with another program. For example open a command prompt, go to C: and enter dir.

  7. #7
    Orange is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    How do you know those files do NOT exist? What program are you using to view the files? Perhaps the files are hidden by the OS. Try looking at the contents of a drive with another program. For example open a command prompt, go to C: and enter dir.
    i didn't use any program to view those files, just with my windows file explorer, that is click directly into my c drive and view it. i tried your suggestion too, my command prompt show me 53 files+folders, but with my JTree, there got 66 files+folders. i tried to use my JFileChooser to view those files, everything is just normal, no non-existed files or folders being seen...

Similar Threads

  1. JcheckBoxes as JTree Nodes
    By aneesahamedaa in forum AWT / Swing
    Replies: 11
    Last Post: 02-11-2009, 12:11 AM
  2. Tooltip not getting displayed...
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 07-31-2008, 10:00 AM
  3. displayed the rownumbers
    By geeta_ravikanti in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 02:30 AM
  4. nodes in java
    By ahsan in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 03:09 PM
  5. Java Components are not displayed(sometimes)
    By archanajathan in forum AWT / Swing
    Replies: 3
    Last Post: 11-05-2007, 08:34 AM

Posting Permissions

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