Results 1 to 2 of 2

Thread: Jtree Help

  1. #1
    miladirooni is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default Jtree Help

    Hi guys

    need help with jTree please?
    I'm trying to create jtree that reads the files from the system, but cant manage to get drives on the panel. and here is what ive got so fare:

    import java.io.File;
    import javax.swing.event.TreeModelListener;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreePath;

    /**
    *
    * @author qyt21516
    */
    public class JpowderTree2 implements TreeModel{



    protected File roots;

    public JpowderTree2(File rts) {
    roots = rts;
    }


    /**
    * Returns the root of the tree. Returns null only if the tree has no nodes.
    * @return
    */
    public Object getRoot() {

    return roots;
    }
    /**
    * Returns the child of parent at index index in the parent's child array.
    * parent must be a node previously obtained from this data source.
    * @param parent
    * @param index
    * @return
    */

    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;
    }
    }
    /**
    * Returns the number of children of parent. Returns 0 if the node is a leaf
    * or if it has no children. parent must be a node previously obtained from
    * this data source.
    * @param parent
    * @return
    */
    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;
    }
    }
    /**
    * Returns true if node is a leaf. It is possible for this method to return false
    * even if node has no children. A directory in a filesystem, for example,
    * may contain no files; the node representing the directory is not a leaf,
    * but it also has no children.
    * @param node
    * @return
    */
    public boolean isLeaf(Object node) {
    File leaf = (File) node;
    return leaf.isFile();
    }
    /**
    *
    * @param path
    * @param newValue
    */
    public void valueForPathChanged(TreePath path, Object newValue) {

    }
    /**
    * Returns the index of child in parent. If parent is null or child is null, returns -1.
    * @param parent
    * @param child
    * @return
    */
    public int getIndexOfChild(Object parent, Object child) {
    String name;
    String[] children = ((File) parent).list();

    if (children.length > 0) {

    name = ((File) child).getName();
    for (int i = 0; i < children.length; i++) {
    if (name.equals(children[i])) {
    return i;
    }
    }
    } else {

    return -1;
    }
    return -1;
    }

    public void addTreeModelListener(TreeModelListener l) {

    }

    public void removeTreeModelListener(TreeModelListener l) {

    }

    }

    this part of my code im pretty sure is good but the next bit im having problem with..


    import java.awt.BorderLayout;
    import java.io.File;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;

    /**
    *
    * @author qyt21516
    */
    public class Jpowder2Driver extends JPanel {


    File drive;
    //private DefaultComboBoxModel comboModel;
    JpowderTree2 model;
    File root = null;
    JTree tree;

    public Jpowder2Driver() {

    DefaultMutableTreeNode top = new DefaultMutableTreeNode(("Computer"));
    File [] rts = File.listRoots();
    for (int i = 0; i < rts.length; i++) {
    drive = rts[i];

    DefaultMutableTreeNode driver = new DefaultMutableTreeNode((new JpowderTree2(rts[i])));
    top.add(driver);
    driver.add(new DefaultMutableTreeNode(
    new Boolean(true)));
    }
    setLayout(new BorderLayout());
    panelB = new JPanel();
    panelB.setLayout(new BorderLayout());
    DefaultTreeModel models = new DefaultTreeModel(top);
    tree = new JTree(models);
    tscrollPane = new JScrollPane(tree);

    panelB.add(tscrollPane, BorderLayout.CENTER);

    add(panelB, BorderLayout.NORTH);
    }


    /**
    * this main method is just for testing purpose
    * @param args
    */
    public static void main(String args[]) {
    JFrame frame = new JFrame("File chooser");
    frame.setDefaultCloseOperation(javax.swing.JFrame. EXIT_ON_CLOSE);
    frame.add(new Jpowder2Driver());
    frame.pack();
    frame.setVisible(true);
    }
    public JPanel panelB;
    public JScrollPane tscrollPane;
    }
    some how i cant manage to add the drive to the Panel

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    cant manage to add the drive to the Panel
    Check the last paragraph in the File class api comments section in the javadocs.
    To test this possibility you could do something like this:
    Java Code:
    import java.io.File;
    
    public class RootRx {
        public static void main(String[] args) {
            File[] files = File.listRoots();
            SecurityManager sm = System.getSecurityManager();
            for(int i = 0; i < files.length; i++) {
                String file = files[i].getName();
                try {
                    sm.checkRead(file);
                } catch(SecurityException se) {
                    System.out.println("SecurityException: " + se.getMessage());
                } catch(NullPointerException npe) {
                    System.out.println("NullPointer: " + npe.getMessage());
                }
                System.out.printf("files[%d]: %s%n", i, file);
            }
        }
    }

Similar Threads

  1. zip to JTree
    By icsbcn in forum AWT / Swing
    Replies: 8
    Last Post: 09-01-2009, 05:26 PM
  2. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 12:59 PM
  3. JTree Popup
    By jiexx in forum New To Java
    Replies: 1
    Last Post: 03-24-2009, 02:58 AM
  4. JTree Problem
    By ngpamela in forum XML
    Replies: 0
    Last Post: 12-10-2008, 05:10 PM
  5. A Simple JTree Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 08:41 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
  •