private static void createAndShowGUI() {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 400);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
root.add(new DefaultMutableTreeNode("child one"));
root.add(new DefaultMutableTreeNode("child two"));
root.add(new DefaultMutableTreeNode("child three"));
JTree tree = new JTree(root);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) (e
.getPath().getLastPathComponent());
System.out.println(node);
}
});
frame.getContentPane().add(tree);
frame.setVisible(true);
}