GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Monday, December 20, 2010

Swing - JTree

Here's a code on how to use JTree:

import javax.swing.*;
import javax.swing.tree.*;


public class Main{

    public static void main(String[] args){

        new Main();
    }

    public Main(){

        frame = new JFrame("JTree Application");

        root = new DefaultMutableTreeNode("World");

        DefaultMutableTreeNode usa = new 
                           DefaultMutableTreeNode("USA");
        root.add(usa);
        DefaultMutableTreeNode id = new   
                           DefaultMutableTreeNode("Indonesia");
        root.add(id);

        DefaultMutableTreeNode ny = new 
                           DefaultMutableTreeNode("New York");
        usa.add(ny);
        DefaultMutableTreeNode ca = new 
                           DefaultMutableTreeNode("California");
        usa.add(ca);

        DefaultMutableTreeNode jkt = new 
                           DefaultMutableTreeNode("Jakarta");
        id.add(jkt);
        DefaultMutableTreeNode dp = new 
                           DefaultMutableTreeNode("Denpasar");
        id.add(dp);

        model = new DefaultTreeModel(root);
        tData = new JTree(model);

        frame.add(new JScrollPane(tData));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    private JFrame frame;
    private JTree tData;  
    private DefaultTreeModel model;
    private DefaultMutableTreeNode root;

Share/Bookmark

No comments:

Post a Comment