Switch to full style
Graphics and animation Java code examples
Post a reply

JTree Usage example , Tree nodes

Thu Feb 07, 2013 10:25 pm

java code
/*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
import com.sun.java.swing.JTree;
//Why do I have to import stuff from the tree package?
//Why Mutable? I don't want to change nodes! Why not DefaultTreeNode?
import com.sun.java.swing.tree.DefaultMutableTreeNode;
import com.sun.java.swing.JScrollPane;
import com.sun.java.swing.JPanel;
import com.sun.java.swing.JFrame;
import java.awt.GridLayout;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;

// It would be cool to modify this so that nodes are links.
public class TreeDemo extends JPanel {
public TreeDemo() {
//Create the nodes.
DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
DefaultMutableTreeNode category;
DefaultMutableTreeNode book;

category = new DefaultMutableTreeNode("Books for Java Programmers");
top.add(category);

//Tutorial
book = new DefaultMutableTreeNode("The Java Tutorial: Object-Oriented Programming for the Internet");
book.add(new DefaultMutableTreeNode("Mary Campione"));
book.add(new DefaultMutableTreeNode("Kathy Walrath"));
category.add(book);

//Arnold/Gosling
book = new DefaultMutableTreeNode("The Java Programming Language");
book.add(new DefaultMutableTreeNode("Ken Arnold"));
book.add(new DefaultMutableTreeNode("James Gosling"));
category.add(book);

//FAQ
book = new DefaultMutableTreeNode("The Java FAQ");
book.add(new DefaultMutableTreeNode("Jonni Kanerva"));
category.add(book);

//Chan/Lee
book = new DefaultMutableTreeNode("The Java Class Libraries: An Annotated Reference");
book.add(new DefaultMutableTreeNode("Patrick Chan"));
book.add(new DefaultMutableTreeNode("Rosanna Lee"));
category.add(book);

//Threads
book = new DefaultMutableTreeNode("Concurrent Programming in Java: Design Principles and Patterns");
book.add(new DefaultMutableTreeNode("Doug Lea"));
category.add(book);

category = new DefaultMutableTreeNode("Books for Java Implementers");
top.add(category);

//VM
book = new DefaultMutableTreeNode("The Java Virtual Machine Specification");
book.add(new DefaultMutableTreeNode("Tim Lindholm"));
book.add(new DefaultMutableTreeNode("Frank Yellin"));
category.add(book);

//Language Spec
book = new DefaultMutableTreeNode("The Java Language Specification");
book.add(new DefaultMutableTreeNode("James Gosling"));
book.add(new DefaultMutableTreeNode("Bill Joy"));
book.add(new DefaultMutableTreeNode("Guy Steele"));
category.add(book);

JTree tree = new JTree(top);

//Create the scroll pane and add the tree to it.
JScrollPane scrollPane = new JScrollPane(tree);

//Add the scroll pane to this panel.
setLayout(new GridLayout(1, 0));
add(scrollPane);
}

public static void main(String[] args) {
/*
* Create a window. Use JFrame since this window will include
* lightweight components.
*/
JFrame frame = new JFrame("TreeDemo");

WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
frame.addWindowListener(l);

frame.getContentPane().add("Center", new TreeDemo());
frame.setSize(500, 200);
frame.show();
}
}




Post a reply
  Related Posts  to : JTree Usage example , Tree nodes
 Take tree nodes then create new tree with another root     -  
 decision tree     -  
 need help for creatinb binary tree in php     -  
 Binary search tree C++     -  
 Java Binary Tree     -  
 java tree problem     -  
 Spanning Tree Algorithm     -  
 Transformations in 3D (Simple EX. Rotated Tree (J2SE)     -  
 java code for decision tree algorithm     -  
 Display data from database as Tree Structure in JSP     -  

Topic Tags

Java Swing