Java JTree get user object on selection event
import java.awt.Dimension; import java.awt.GridLayout; import java.net.URL; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; public class Main extends JPanel implements TreeSelectionListener { private JTree tree; private static String lineStyle = "Horizontal"; public Main() { super(new GridLayout(1, 0)); // Create the nodes. DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series"); createNodes(top);// w ww .j a va 2 s . c o m tree = new JTree(top); tree.addTreeSelectionListener(this); tree.putClientProperty("JTree.lineStyle", lineStyle); JScrollPane treeView = new JScrollPane(tree); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setTopComponent(treeView); Dimension minimumSize = new Dimension(100, 50); treeView.setMinimumSize(minimumSize); splitPane.setDividerLocation(100); splitPane.setPreferredSize(new Dimension(500, 300)); add(splitPane); } public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (node == null) return; Object nodeInfo = node.getUserObject(); if (node.isLeaf()) { BookInfo book = (BookInfo) nodeInfo; System.out.print(book.bookURL); } System.out.println(nodeInfo.toString()); } private void createNodes(DefaultMutableTreeNode top) { DefaultMutableTreeNode category = null; DefaultMutableTreeNode book = null; category = new DefaultMutableTreeNode("Books Programmers"); top.add(category); book = new DefaultMutableTreeNode(new BookInfo("C++", "Cpp")); category.add(book); book = new DefaultMutableTreeNode(new BookInfo("C", "c.html")); category.add(book); book = new DefaultMutableTreeNode(new BookInfo("Python", "python.html")); category.add(book); book = new DefaultMutableTreeNode(new BookInfo("SQL", "sql.html")); category.add(book); book = new DefaultMutableTreeNode(new BookInfo("HTML", "html.html")); category.add(book); book = new DefaultMutableTreeNode(new BookInfo("Javascript", "javascript.html")); category.add(book); category = new DefaultMutableTreeNode("B1"); top.add(category); book = new DefaultMutableTreeNode(new BookInfo("CSS", "css.html")); category.add(book); book = new DefaultMutableTreeNode(new BookInfo("Java", "java.html")); category.add(book); } public static void main(String[] args) { JFrame frame = new JFrame("TreeDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Main()); frame.pack(); frame.setVisible(true); } } class BookInfo { public String bookName; public URL bookURL; public BookInfo(String book, String filename) { bookName = book; bookURL = getClass().getResource(filename); if (bookURL == null) { System.err.println("Couldn't find file: " + filename); } } public String toString() { return bookName; } }