0

is there a quick and dirty way to populate a jtree ( root and child nodes ) of a specified package?

so for instance:

com.mycompany.components.ComponentA
com.mycompany.components.ComponentB
com.mycompany.components.ComponentC

Is there a way in pseudo code:

Jtree tree = new JTree( Package.getPackage("com.mycompany") );

So the final result is:

com.mycompany.components
|---- com.mycompany.components.ComponentA
|---- com.mycompany.components.ComponentB
|---- com.mycompany.components.ComponentC

???

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
delita
  • 1,571
  • 1
  • 19
  • 25

1 Answers1

2

is there a quick and dirty way to populate a jtree ( root and child nodes ) of a specified package?

Yes. Java packages by Netbeans project structure:

enter image description here

import java.awt.*;
import java.io.File;
import java.util.Collections;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

public class FileTree extends JPanel {

    private static final long serialVersionUID = 1L;

    public FileTree(File dir) {
        setLayout(new BorderLayout());
        JTree tree = new JTree(addNodes(null, dir));
        tree.addTreeSelectionListener(new TreeSelectionListener() {

            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                System.out.println("You selected " + node);
            }
        });
        JScrollPane scrollpane = new JScrollPane();
        scrollpane.getViewport().add(tree);
        add(BorderLayout.CENTER, scrollpane);
    }

    private DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
        String curPath = dir.getPath();
        DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
        if (curTop != null) {
            curTop.add(curDir);
        }
        Vector<String> ol = new Vector<String>();
        String[] tmp = dir.list();
        for (int i = 0; i < tmp.length; i++) {
            ol.addElement(tmp[i]);
        }
        Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
        File f;
        Vector<Object> files = new Vector<Object>();
        for (int i = 0; i < ol.size(); i++) {
            String thisObject = ol.elementAt(i);
            String newPath;
            if (curPath.equals(".")) {
                newPath = thisObject;
            } else {
                newPath = curPath + File.separator + thisObject;
            }
            if ((f = new File(newPath)).isDirectory()) {
                addNodes(curDir, f);
            } else {
                files.addElement(thisObject);
            }
        }
        for (int fnum = 0; fnum < files.size(); fnum++) {
            curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
        }
        return curDir;
    }

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(200, 400);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 400);
    }

    public static void main(final String[] av) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame("FileTree");
                frame.setForeground(Color.black);
                frame.setBackground(Color.lightGray);
                Container cp = frame.getContentPane();
                if (av.length == 0) {
                    cp.add(new FileTree(new File(".")));
                } else {
                    cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
                    for (int i = 0; i < av.length; i++) {
                        cp.add(new FileTree(new File(av[i])));
                    }
                }
                frame.pack();
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });
    }
}

EDIT

you can remove all JTree Icons, for example JTree with NoIcon

DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Now do that with classes from a Jar (or Jars) in an applet or JWS app. :D +1 – Andrew Thompson May 19 '12 at 07:09
  • @Andrew Thompson phaaa [do you meaning](http://stackoverflow.com/questions/6629995/test-if-a-class-contains-an-instance-variable-based-on-its-name) – mKorbel May 19 '12 at 07:11
  • I was thinking more like [*Strictly speaking, it isn't possible to list the classes in a package*](http://stackoverflow.com/a/3527428/418556). :) – Andrew Thompson May 19 '12 at 07:14
  • no idea after wake-up my body is still in the safety mode, fell free for edit(s), better to post that an answer here, last brain's cell died :-) – mKorbel May 19 '12 at 07:29
  • 1
    I should never have brought up icons. It is a great little example. :) – Andrew Thompson May 19 '12 at 08:11