1. Create a new project or open your exam project
2. Create a new JFrame class, in this case i called JTreeFolder
3. Drag, Drop and design form like this:

and this in Inspector:

4. Open "Source" tab and goto constructor:
public JTreeFolder() {
initComponents();
}under:initComponents();write this code:
File dir = new File("C:/windows");
DefaultMutableTreeNode root = new DefaultMutableTreeNode(dir);
scan(dir, root);
tree.setModel(new DefaultTreeModel(root));5. We need to create one method to scan folder, this method is run recursively:private void scan(File file, DefaultMutableTreeNode root) {
for (File f : file.listFiles()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(f.getName());
root.add(node);
if (f.isDirectory()) {
scan(f, node);
}
}
}6. Finally run the project and we get this:
Conclusion:
To representation node in JTree simple way is using DefaultMutableTreeNode.
Complete Sourcecode:
/*
* JTreeFolder.java
*
* Created on 16 Juli 2008, 22:07
*/
package org.kazao.tips.tip006;
import java.io.File;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
/**
*
* @author Mr. Kazao
*/
public class JTreeFolder extends javax.swing.JFrame {
/** Creates new form JTreeFolder */
public JTreeFolder() {
initComponents();
File dir = new File("C:/windows");
DefaultMutableTreeNode root = new DefaultMutableTreeNode(dir);
scan(dir, root);
tree.setModel(new DefaultTreeModel(root));
}
private void scan(File file, DefaultMutableTreeNode root) {
for (File f : file.listFiles()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(f.getName());
root.add(node);
if (f.isDirectory()) {
scan(f, node);
}
}
}
/** 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.
*/
@SuppressWarnings("unchecked")
////GEN-BEGIN:initComponents //GEN-END:initComponents
private void initComponents() {
scroller = new javax.swing.JScrollPane();
tree = new javax.swing.JTree();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Using JTree to scaning folder");
scroller.setName("scroller"); // NOI18N
tree.setName("tree"); // NOI18N
scroller.setViewportView(tree);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(scroller, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(scroller, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}//
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JTreeFolder().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
public javax.swing.JScrollPane scroller;
public javax.swing.JTree tree;
// End of variables declaration//GEN-END:variables
}

1 komentar:
That is an excellent tutorial.
Would you like your blog to gain more popularity? Please visit my blog at http://expandforfree.blogspot.com which provides a free Blog Expansion service.
Post a Comment