Q:

I can't change card in cardLayout using JTree listener

I have problem with changing card in CardLayout. I have a JPanel with CardLayout as layout, and other JPanel which contains JPanel with JTree (in other classs). I want to add TreeSelectionListener. If I click on the node, card should change to the node title.
Here is some code:
The listener from which I want to change cards is in the second code snippet under line 159.
When I'm changing the card from the ThumbPanel (class with JPanel and CardLayout as layout), it works, but the TreeSelectionListener is in other class, and when I try to change it from there, nothing happens.
JPanel with cardlayout:
import com.alee.extended.image.WebImage;
import com.alee.managers.tooltip.TooltipManager;
import com.alee.managers.tooltip.TooltipWay;

    import javax.swing.*;
    import javax.swing.border.Border;
    import java.awt.*;

    public class ThumbPanel extends JPanel{
    public CardLayout cl = new CardLayout();

        public ThumbPanel(){
            super();
            initComponents();

        }

        public void initComponents() {
            final ThumbPopupMenu tpm = new ThumbPopupMenu();
            setMinimumSize(new Dimension(200, 320));
            setPreferredSize(new Dimension(650, 400));
            setVisible(true);
            setLayout(cl);
            Border blackline = BorderFactory.createLineBorder(Color.black);
            setBorder(blackline);
            JPanel p1 = new JPanel();
            p1.add(new JLabel("parent 1"));
            JPanel p2 = new JPanel();
            p2.add(new JLabel("parent 2"));
            add(p1, "Parent 1");
            add(p2, "Parent 2");
            }

        public void showCategoryPanel(String panelName){
            cl.show(this, panelName);
        }
    }

Here is the JTreeHelper, which contains TreeSelectionListener (on the bottom fo this code)
    import java.awt.CardLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.util.Enumeration;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.MutableTreeNode;
    import javax.swing.tree.TreePath;
    import javax.swing.tree.TreeSelectionModel;
    import javax.swing.event.TreeModelEvent;
    import javax.swing.event.TreeModelListener;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import net.miginfocom.swing.MigLayout;

    public class JTreeHelper extends JPanel{
    protected DefaultMutableTreeNode rootNode;
    protected DefaultTreeModel treeModel;
    protected JTree tree;
    private Toolkit toolkit = Toolkit.getDefaultToolkit();
    private ThumbPanel tp = new ThumbPanel();
    public JTreeHelper() {
        super();
        ml = new MigLayout("",
                "[]",
                "[]");
        setLayout(ml);
        setMinimumSize(new Dimension(200, 320));
        setPreferredSize(new Dimension(250, 400));
        rootNode = new DefaultMutableTreeNode("Root Node");
        treeModel = new DefaultTreeModel(rootNode);
    treeModel.addTreeModelListener(new MyTreeModelListener());

        tree = new JTree(treeModel);
        tree.setEditable(true);
        tree.getSelectionModel().setSelectionMode
                (TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setShowsRootHandles(true);

        //add JTree listeners
        tree.addTreeSelectionListener(createSelectionListener());

        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setMinimumSize(new Dimension(200, 320));
        scrollPane.setPreferredSize(new Dimension(250, 400));
        scrollPane.setBorder(null);
        add(scrollPane);

    }

    /** Remove all nodes except the root node. */
    public void clear() {
        rootNode.removeAllChildren();
        treeModel.reload();
    }

    /** Remove the currently selected node. */
    public void removeCurrentNode() {
        TreePath currentSelection = tree.getSelectionPath();
        if (currentSelection != null) {
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
                         (currentSelection.getLastPathComponent());
            MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
            if (parent != null) {
                treeModel.removeNodeFromParent(currentNode);
                return;
            }
        } 

        // Either there was no selection, or the root was selected.
        toolkit.beep();
    }

    /** Add child to the currently selected node. */
    public DefaultMutableTreeNode addObject(Object child) {
        DefaultMutableTreeNode parentNode = null;
        TreePath parentPath = tree.getSelectionPath();

        if (parentPath == null) {
            parentNode = rootNode;
        } else {
            parentNode = (DefaultMutableTreeNode)
                         (parentPath.getLastPathComponent());
        }

        return addObject(parentNode, child, true);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child) {
        return addObject(parent, child, false);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child, 
                                            boolean shouldBeVisible) {
        DefaultMutableTreeNode childNode = 
                new DefaultMutableTreeNode(child);

        if (parent == null) {
            parent = rootNode;
        }

    //It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
        treeModel.insertNodeInto(childNode, parent, 
                                 parent.getChildCount());

        //Make sure the user can see the new node.
        if (shouldBeVisible) {
            tree.scrollPathToVisible(new TreePath(childNode.getPath()));
        }
        return childNode;
    }

    class MyTreeModelListener implements TreeModelListener {
        public void treeNodesChanged(TreeModelEvent e) {
            DefaultMutableTreeNode node;
            node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent());
            int index = e.getChildIndices()[0];
            node = (DefaultMutableTreeNode)(node.getChildAt(index));

            System.out.println("The user has finished editing the node.");
            System.out.println("New value: " + node.getUserObject());
        }
        public void treeNodesInserted(TreeModelEvent e) {
        }
        public void treeNodesRemoved(TreeModelEvent e) {
        }
        public void treeStructureChanged(TreeModelEvent e) {
        }
    }
        /*tree selection listener
         * 
         * after click on tree node, the category is shown on the right
         * 
         * 
         */

        private TreeSelectionListener createSelectionListener() {
            return new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                TreePath path = e.getPath();                
                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
               // HERE SHOULD CHANGE CARD
                tp.cl.show(tp, parentNode.toString());
                System.out.println(parentNode.toString());

            }

        };
    }

    /*
     * 
     * Method which finds JTree node with given name
     * 
     */    
    public TreePath find(DefaultMutableTreeNode root, String s) {
        @SuppressWarnings("unchecked")
        Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
        while (e.hasMoreElements()) {
            DefaultMutableTreeNode node = e.nextElement();
            if (node.toString().equalsIgnoreCase(s)) {
                return new TreePath(node.getPath());
            }
        }
        return null;
    }

}

This is the third card, which just adds JTree to the gui.
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.Border;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreePath;
    import javax.xml.ws.BindingProvider;
    import net.miginfocom.swing.MigLayout;

    public class CategoryTreePanel extends JPanel{

    private JTreeHelper treePanel;

    public CategoryTreePanel(){
        super();
        initComponents();
    }

    public void initComponents() {

        setMinimumSize(new Dimension(200, 320));
        setPreferredSize(new Dimension(250, 400));
        setVisible(true);

        ml = new MigLayout("",
                "[]",
                "[]");
        setLayout(ml);
        Border blackline = BorderFactory.createLineBorder(Color.black);
        setBorder(blackline);
        treePanel = new JTreeHelper();
        populateTree(treePanel, thumbPanel);
        add(treePanel);
    }

    public void populateTree(JTreeHelper treePanel, JPanel mainPanel) {

        String p1Name = new String("Parent 1");
        String p2Name = new String("Parent 2");
        String c1Name = new String("Child 1");
        String c2Name = new String("Child 2");
        String[] categoryArray = {p1Name, p2Name,c1Name,c2Name};
        for (String s : categoryArray){
            if(s.equals(p1Name) == true){
                createNode(s, true, null, mainPanel);
            }
            if(s.equals(p2Name) == true){
                createNode(s, true, null, mainPanel);
            }
            if(s.equals(c1Name) == true){
                createNode(s, false, "Parent 1", mainPanel);
            }
            if(s.equals(c2Name) == true){
                createNode(s, false, "Parent 2", mainPanel);
            }

        }
        /*
         * DefaultMutableTreeNode p1, p2;

        p1 = treePanel.addObject(null, p1Name);
        p2 = treePanel.addObject(null, p2Name);

        treePanel.addObject(p1, c1Name);
        treePanel.addObject(p1, c2Name);

        treePanel.addObject(p2, c1Name);
        treePanel.addObject(p2, c2Name);
        */

    }

    public void createNode(String categoryName, boolean isParent, String parent, 
        JPanel mainPanel){
        DefaultMutableTreeNode categoryNode;
        if(isParent == true){

            categoryNode = treePanel.addObject(null, categoryName);

        }else{

            DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode)               treePanel.tree.getModel().getRoot();
            TreePath path = treePanel.find(rootNode, parent);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            categoryNode = treePanel.addObject(parentNode, categoryName);

        }
    }

    }

Any ideas how could I change cards in ThumbPanel from the listener in JTreeHelper ?

A:

Check what is returned by the 
parentNode.toString()

Try instead
tp.cl.show(tp, parentNode.getUserObject().toString());

A:

The problem is solved, I have put all code in one class and now it's working. I don't know why I couldn't change cards when I have the code in three different classes.
Now it looks terrible, but it's working and I can move on with my project. 
Thank you all for your time.
Best Regards 
MK

