Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:39:50+00:00 2026-06-16T02:39:50+00:00

I have problem with changing card in CardLayout. I have a JPanel with CardLayout

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:39:51+00:00Added an answer on June 16, 2026 at 2:39 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started creating android apps, but i have a problem changing the layout
I have a problem for changing the displayed view. My app contains 2 views
I have some CheckBoxPreferences and I have no problem changing the icon for them
HI! I have a problem with changing the name of a select element. I
I have the following code, and i have a problem regarding changing the ringtone
I have an strange problem with sencha touch. Changing the value in the select
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I'm using the Tropo MVC classes and have a problem with changing the voice
I'm using the jQuery datepicker from jqueryui.com and I have a problem changing the
I have a problem with changing the color of a button (in vb.net). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.