JTree with TreeSelectionListener and TreeSelectionEvent

A TreeSelectionEvent is generated when the user selects or deselects a node within the tree. The listeners for these events are TreeSelectionListener. The tree event classes and listener interfaces are packaged in javax.swing.event. You can obtain the path to the selected object by calling getPath( ), shown here, on the event object.

TreePath getPath( )

It returns a TreePath object that describes the path to the changed node. The TreePath class encapsulates information about a path to a particular node in a tree.

Program

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import java.awt.Dimension;
import java.awt.FlowLayout;

class Frame extends JFrame implements TreeSelectionListener{
  
    private JTree jt;
    private JTextArea jta;
    
    Frame()
    {
        setTitle("JTree");
        setLayout(new FlowLayout());
        setJTree();
        setJTextArea();
        setAction();
        setSize(700,350);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void setJTree()
    {
        DefaultMutableTreeNode main = new DefaultMutableTreeNode("List of programming "
                                                                +"languages by type");
        createTree(main);
        jt = new JTree(main);
        JScrollPane js = new JScrollPane(jt);
        js.setPreferredSize(new Dimension(300,300));
        add(js);  
    }
    
    private void setJTextArea()
    {
        jta = new JTextArea();
        JScrollPane js = new JScrollPane(jta);
        js.setPreferredSize(new Dimension(300,300));
        add(js); 
    }
    
    private void createTree(DefaultMutableTreeNode main)
    {
        DefaultMutableTreeNode sub1 = new DefaultMutableTreeNode("Assembly languages");
        sub1.add(new DefaultMutableTreeNode("Autocoder"));
        sub1.add(new DefaultMutableTreeNode("Linoleum"));
        sub1.add(new DefaultMutableTreeNode("Sphinx C"));
        DefaultMutableTreeNode sub2 = new DefaultMutableTreeNode("Procedural languages");
        sub2.add(new DefaultMutableTreeNode("C"));
        sub2.add(new DefaultMutableTreeNode("Fortran"));
        sub2.add(new DefaultMutableTreeNode("Pascal"));
        DefaultMutableTreeNode sub3 = new DefaultMutableTreeNode("Object-oriented languages");
        sub3.add(new DefaultMutableTreeNode("C++"));
        sub3.add(new DefaultMutableTreeNode("Java"));
        sub3.add(new DefaultMutableTreeNode("Ruby"));
        main.add(sub1);
        main.add(sub2);
        main.add(sub3);
    }
    
    private void setAction()
    {
        jt.addTreeSelectionListener(this);
    }
    
    public void valueChanged(TreeSelectionEvent eve)
    {
        String getPath = String.valueOf(jt.getSelectionPath());
        if(getPath.equals("[List of programming languages by type, Assembly languages, Autocoder]"))
        {
            jta.setText("Autocoder was the name given to certain n"
                       +"assemblers for a number of IBM computers n"
                       +"of the 1950s and 1960s. The first Autocoders n"
                       +"appear to have been the earliest assemblers to n"
                       +"provide a macro facility.");
        }
        else if(getPath.equals("[List of programming languages by type, Assembly languages, Linoleum]"))
        {
            jta.setText("The L.in.oleum (often called Linoleum or n"
                       +"simply Lino) is an unstructured, untyped, n"
                       +"procedural programming language and a cross-platform n"
                       +"assembler developed by Italian programmer n"
                       +"Alessandro Ghignola beginning in 2001.");
        }
        else if(getPath.equals("[List of programming languages by type, Assembly languages, Sphinx C]"))
        {
            jta.setText("C-- (see minus minus) is a C-like programming n"
                       +"language. Its creators, functional programming n"
                       +"researchers Simon Peyton Jones and Norman Ramsey, n"
                       +"designed it to be generated mainly by compilers for n"
                       +"very high-level languages rather than written by n"
                       +"human programmers.");
        }
        else if(getPath.equals("[List of programming languages by type, Procedural languages, C]"))
        {
            jta.setText("In computing, C is a general-purpose programming n"
                       +"language initially developed by Dennis Ritchie n"
                       +"between 1969 and 1973 at AT&T Bell Labs.");
        }
        else if(getPath.equals("[List of programming languages by type, Procedural languages, Fortran]"))
        {
            jta.setText("Fortran (previously FORTRAN, derived from Formula n"
                       +"Translating System) is a general-purpose, imperative n"
                       +"programming language that is especially suited to n"
                       +"numeric computation and scientific computing.");
        }
        else if(getPath.equals("[List of programming languages by type, Procedural languages, Pascal]"))
        {
            jta.setText("Pascal is an influential imperative and procedural n"
                       +"programming language, designed in 1968–1969 and n"
                       +"published in 1970 by Niklaus Wirth as a small and n"
                       +"efficient language intended to encourage good n"
                       +"programming practices using structured programming n"
                       +"and data structuring.");
        }
        else if(getPath.equals("[List of programming languages by type, Object-oriented languages, C++]"))
        {
            jta.setText("C++ (pronounced see plus plus) is a general purpose n"
                       +"programming language that is free-form and compiled. n"
                       +"it is regarded as an intermediate-level language n"
                       +"features. It provides imperative, object-oriented n"
                       +"and generic programming features.");
        }
        else if(getPath.equals("[List of programming languages by type, Object-oriented languages, Java]"))
        {
            jta.setText("Java is a computer programming language that is concurrent, n"
                       +"class-based, object-oriented, and specifically designed n"
                       +"to have as few implementation dependencies as possible. ");
        }
        else if(getPath.equals("[List of programming languages by type, Object-oriented languages, Ruby]"))
        {
            jta.setText("Ruby is a dynamic, reflective, object-oriented, n"
                       +"general-purpose programming language. It was designed n"
                       +"and developed in the mid-1990s by Yukihiro Matsumoto n"
                       +"in Japan.");
        }
    }
}

public class Javaapp {
   
    public static void main(String[] args) {
        
       Frame fr = new Frame();
    }
}


Program Output

Leave a Comment