Thursday, July 17, 2008

Using JTree

JTree is a tree component in swing technology, JTree usually used to representation of tree hierarchy like folder on Windows Explorer. This tutorial is show you howto showing folder hierarchy using JTree component.
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
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();
}//
//GEN-END:initComponents

/**
* @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
}
Diposting oleh M. Jumari di Thursday, July 17, 2008
Label: English, Java, JTree, Swing, Tips and Tricks, Tutorial

1 komentar:

ExpandForFree said...

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.

July 17, 2008 1:35 PM

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

About Me

My Photo
My name is M. Jumari @cyberworld i'am also known as Kazao aka Kazao™ aka Mr. Kazao. I'am a simply man enjoying life in a beautifull country.
     All source code you get from here is absolutely free. You may view my complete profile or view my website.

Messenger

m.jumari

Archive

  • April 2009 (6)
  • March 2009 (1)
  • January 2009 (3)
  • December 2008 (4)
  • October 2008 (1)
  • September 2008 (5)
  • August 2008 (12)
  • July 2008 (20)
  • May 2008 (2)

Labels

  • Bahasa Indonesia (29)
  • Berita (1)
  • Cuap-Cuap (3)
  • Curhat (10)
  • English (18)
  • Filosofi (1)
  • Foto (2)
  • Gambar (5)
  • Java (17)
  • Joke (2)
  • JPanel (2)
  • JTable (2)
  • JTextArea (1)
  • JTree (3)
  • Printing (2)
  • Reflection (1)
  • Swing (10)
  • Tips and Tricks (21)
  • Trojan (1)
  • Tutorial (17)
  • Video (1)
  • Virus (1)

Friends

  • Tikabanget™
    Hello world!
    1 week ago
  • Herman Saksono
    Siapa Mau Sewa Mario Ozawa?
    1 week ago
  • Herwin Saputra
    Terdampar
    2 weeks ago
  • Mama Vino
    KBB#18 – French Macarons
    5 weeks ago
  • Obie XP
    819936000 + n detik (n=n+1)
    4 months ago
  • Oktarianti
    Banda Aceh
    6 months ago
  • Indra Wahyudi
    Pesan Itu Mengisyaratkan "BAHAYA"
    1 year ago
  • A r l e y N o v a ™
    why marry?
    1 year ago
  • Dony Iswantoro
    Tentang seorang "Pangeran"
    1 year ago
  • Akhmad Fathonih
    Finally, a proper kde 4.2
    1 year ago
  • The Vemo™
    Terima Kasih
    1 year ago
  • Harvey
    Kekuatan Maaf
    2 years ago
  • Teguh Susanto
    Bermain Seks Gunakan Otak....
    2 years ago
Show 5 Show All

Links

  • artikel mantab

Subscribe

Posts
Atom
Posts
Comments
Atom
Comments

Ketawa.com

Loading...

World Clocks

Followers

Widgets

Google Groups
Berlangganan ke informatika98
Email:
Kunjungi grup ini
Google Groups
informatics-uii
Kunjungi grup ini
Kazao™

YouTube

Loading...
Copyright © M. Jumari 2008 Powered by www.blogger.com
Visit my web @ mr.kazao.net and you can call me @ +6281904091661 or +622743251763