Erm a tree of file directory?

hi, i have a application, i have to create something likeFile - > new project for wateva im doing -> the user will find a directory -> click ok and it will show up in the tree, like how it is done on the netbeans IDE does anyone know how i can achieve that? =x
[290 byte] By [Alandera] at [2007-11-27 2:24:43]
# 1
1. Don't use SMS language2. Read a tutorial on using file chooser and ask a specific question about code that doesn't work.
kirillga at 2007-7-12 2:32:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

a small example for you. if you want to list the files of an directory follow this steps:

1. set your fileChooser to: fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

2. Walk in an recursion through this selected Directory and at it to the filelist.

have fun, marcel

/*

* FileChooserDemo.java

*

* Created on 26. April 2007, 10:02

*/

package tests1;

/**

*

* @author hecker_m

*/

import javax.swing.*;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.DefaultTreeModel;

public class FileChooserDemo extends javax.swing.JFrame {

//MyAttributes

DefaultMutableTreeNode fileroot = new DefaultMutableTreeNode("lets go");

DefaultTreeModel filemodel = new DefaultTreeModel(fileroot);

/** Creates new form FileChooserDemo */

public FileChooserDemo() {

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();

fileTree = new javax.swing.JTree();

jMenuBar1 = new javax.swing.JMenuBar();

jMenu1 = new javax.swing.JMenu();

OpenDirectory = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

fileTree.setModel(filemodel);

jScrollPane1.setViewportView(fileTree);

jMenu1.setText("File");

OpenDirectory.setText("Open");

OpenDirectory.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

OpenDirectoryActionPerformed(evt);

}

});

jMenu1.add(OpenDirectory);

jMenuBar1.add(jMenu1);

setJMenuBar(jMenuBar1);

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()

.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 183, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addContainerGap(217, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)

);

pack();

}// </editor-fold>

private void OpenDirectoryActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

JFileChooser fc = new JFileChooser(System.getProperty("user.home"));

fc.setDialogTitle("Select a File");

fc.setDialogType(JFileChooser.OPEN_DIALOG);

fc.setMultiSelectionEnabled(false);

if (fc.showOpenDialog(this) == fc.APPROVE_OPTION) {

String filename = fc.getSelectedFile().getName();

fileroot.add(new DefaultMutableTreeNode(filename));

filemodel.reload();

}

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new FileChooserDemo().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JMenuItem OpenDirectory;

private javax.swing.JTree fileTree;

private javax.swing.JMenu jMenu1;

private javax.swing.JMenuBar jMenuBar1;

private javax.swing.JScrollPane jScrollPane1;

// End of variables declaration

}

Message was edited by:

marcel_hecker

marcel_heckera at 2007-7-12 2:32:05 > top of Java-index,Desktop,Core GUI APIs...