Java JTree_1

1468 ワード

package com.han;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class JTree_1 extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2628449366554214781L;
	public JTree_1() {
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("   ");
		DefaultMutableTreeNode nodeFirst = new DefaultMutableTreeNode("     A");
		root.add(nodeFirst);
		DefaultMutableTreeNode nodeSecond = new DefaultMutableTreeNode("     ", false);
		nodeFirst.add(nodeSecond);
		root.add(new DefaultMutableTreeNode("     B"));
		JTree treeRoot = new JTree(root);
		getContentPane().add(treeRoot, BorderLayout.WEST);
		
		DefaultTreeModel defaultTreeModel = new DefaultTreeModel(root);
		JTree treeDefault = new JTree(defaultTreeModel);
		getContentPane().add(treeDefault, BorderLayout.CENTER);
		
		// recommended creating method
		DefaultTreeModel pointedTreeModel = new DefaultTreeModel(root, true);
		JTree treePointed = new JTree(pointedTreeModel);
		getContentPane().add(treePointed, BorderLayout.EAST);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JTree_1 instance = new JTree_1();
		instance.setVisible(true);
		instance.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		instance.pack();
	}

}