by Dinesh Thakur Category: Swing

Using a JTree is as simple as saying:

add(new JTree( new Object[] {"One", "Two", "Three"}));
This displays a primitive tree. The API is huge tree without But, certainly one of the longest of Swing. It seems that you can do everything with trees, but the most sophisticated tasks may require quite a bit of research and experimentation.

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
import javax.swing.tree.*;
public class JavaExampleTreeEventInJApplet extends JApplet
    public void init()
        JTree Tree = new JTree();
        getContentPane().add(new JScrollPane(Tree));
        Tree.addMouseListener(new MouseAdapter()
               public void mousePressed(MouseEvent e1)
                     String OutStrng = null;
                     JTree Tree=(JTree)e1.getSource();
                     int ClckdRow = Tree.getRowForLocation(e1.getX(), e1.getY());
                     if(ClckdRow != -1)
                            TreePath TreePth = Tree.getPathForRow(ClckdRow);
                            TreeNode TreeNd = (TreeNode)TreePth.getLastPathComponent();
                            OutStrng = "Node " + TreeNd.toString();
                             if(e1.getClickCount() == 1)
                                   OutStrng += "Clicked Single.";
                                         OutStrng += "Clicked Double.";
/*<APPLET CODE =JavaExampleTreeEventInJApplet.class WIDTH = 35  HEIGHT = 280 ></APPLET>*/

JTree Events Example in Java

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles