This discussion is archived
1 Reply Latest reply: Jun 27, 2013 12:56 AM by mKorbel RSS

JTree Action Listener Issue

952174 Newbie
Currently Being Moderated

Hi,

 

We have created a JTree using below logic. We have two java files.

 

MyTreeModelListener.java which will control the tree creation and Tree Action handler logic.

Main.java which will control frame creation and adding the tree to frame.

 

Issue: If I try to expand/collapse the node, unable to invoke listener methods.

Please correct me, if iam doing some thing wrong.

 

MyTreeModelListener

=================

package com.oracle.appsfdoc.fusion.sos;

import javax.swing.JTree;

import javax.swing.event.TreeExpansionEvent;

import javax.swing.event.TreeExpansionListener;

import javax.swing.event.TreeSelectionEvent;

import javax.swing.event.TreeWillExpandListener;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.DefaultTreeModel;

 

public class MyTreeModelListener extends DefaultTreeModel implements TreeExpansionListener,

                                                        TreeWillExpandListener{

    final DefaultMutableTreeNode root;

    DefaultMutableTreeNode parentA,parentB;

    public MyTreeModelListener(){

        super(new DefaultMutableTreeNode("ROOT"));

        root = (DefaultMutableTreeNode) super.getRoot();

        JTree tree = new JTree(root);

        parentA = new DefaultMutableTreeNode("A");

        parentB = new DefaultMutableTreeNode("B");

        root.add(parentA);

        root.add(parentB);

        parentA.add(new DefaultMutableTreeNode("C"));

        parentB.add(new DefaultMutableTreeNode("D"));

        tree.addTreeExpansionListener(this);

        tree.addTreeWillExpandListener(this);

    }   

    public void treeExpanded(TreeExpansionEvent event) {

        System.out.println("treeExpanded");

    }

 

    public void treeCollapsed(TreeExpansionEvent event) {

        System.out.println("treeCollapsed");

    }

 

    public void treeWillExpand(TreeExpansionEvent event) {

        System.out.println("treeWillExpand");

    }

 

    public void treeWillCollapse(TreeExpansionEvent event) {

        System.out.println("treeWillCollapse");

    }

}

 

Main.java

===========

package com.oracle.appsfdoc.fusion.sos;

 

import java.awt.BorderLayout;

 

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTree;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.DefaultTreeModel;

 

public class Main {

   

    public static void main(String args[]) {

        JFrame frame = new JFrame();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final DefaultMutableTreeNode root;

        MyTreeModelListener mListener = new MyTreeModelListener();

        root = (DefaultMutableTreeNode)mListener.getRoot();

        JTree tree = new JTree(root);

        JScrollPane scrollPane = new JScrollPane(tree);

        frame.add(scrollPane, BorderLayout.CENTER);

        frame.setSize(300, 150);

        frame.setVisible(true);

    }

}

 

Thanks in advance,

MSR.

  • 1. Re: JTree Action Listener Issue
    mKorbel Explorer
    Currently Being Moderated

    1. you shadowing variables betweens classes

     

    2. listener isn't proper place, constructor for creating those Objects

     

    3. please see diff

     

    import javax.swing.JFrame;

    import javax.swing.JScrollPane;

    import javax.swing.JTree;

    import javax.swing.SwingUtilities;

    import javax.swing.event.TreeExpansionEvent;

    import javax.swing.event.TreeExpansionListener;

    import javax.swing.event.TreeWillExpandListener;

    import javax.swing.tree.DefaultMutableTreeNode;

     

    public class JTreeAndTreeListeners {

     

        private JFrame frame = new JFrame();

        private JTree tree;

        private DefaultMutableTreeNode root;

        private DefaultMutableTreeNode parentA, parentB;

     

        public JTreeAndTreeListeners() {

            root = new DefaultMutableTreeNode();

            parentA = new DefaultMutableTreeNode("A");

            parentB = new DefaultMutableTreeNode("B");

            root.add(parentA);

            root.add(parentB);

            parentA.add(new DefaultMutableTreeNode("C"));

            parentB.add(new DefaultMutableTreeNode("D"));

            tree = new JTree(root);

            tree.addTreeExpansionListener(new MyTreeModelListener());

            tree.addTreeWillExpandListener(new MyTreeModelListener());

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JScrollPane scrollPane = new JScrollPane(tree);

            frame.add(scrollPane);

            frame.pack();

            frame.setVisible(true);

        }

     

        public static void main(String[] args) {

            SwingUtilities.invokeLater(new Runnable() {

                @Override

                public void run() {

                    new JTreeAndTreeListeners();

                }

            });

        }

     

        class MyTreeModelListener implements TreeExpansionListener,

                TreeWillExpandListener {

     

            @Override

            public void treeExpanded(TreeExpansionEvent event) {

                System.out.println("treeExpanded");

            }

     

            @Override

            public void treeCollapsed(TreeExpansionEvent event) {

                System.out.println("treeCollapsed");

            }

     

            @Override

            public void treeWillExpand(TreeExpansionEvent event) {

                System.out.println("treeWillExpand");

            }

     

            @Override

            public void treeWillCollapse(TreeExpansionEvent event) {

                System.out.println("treeWillCollapse");

            }

        }

    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points