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

  • SEARCH
  • Home
  • 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 8166065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:44:01+00:00 2026-06-06T19:44:01+00:00

I have a problem with expanding JTree nodes. I start when use selects a

  • 0

I have a problem with expanding JTree nodes. I start when use selects a node everything but the path to the selected node and the selected node itself to be collapsed. I’ve tried using

tree.collapsePath(new TreePath(tree.getModel().getRoot()));
tree.expandPath(new TreePath(e111.getPath()));

something like this but it seems to have no effect and I really have no idea how can I do this

Here’s how my tree looks:

http://img220.imageshack.us/img220/3450/jtreepng.png

If I click 1.1.1 I want everything else to be collapsed but the 1.1.1 node and the elements from the path to it.

Here’s the application I’ve created:

public class SelectableTree extends JFrame implements TreeSelectionListener {
public static void main(String[] args) {
    new SelectableTree();
}

DefaultMutableTreeNode root;
DefaultMutableTreeNode e1;
DefaultMutableTreeNode e2;
DefaultMutableTreeNode e3;

DefaultMutableTreeNode e11;
DefaultMutableTreeNode e22;
DefaultMutableTreeNode e33;

DefaultMutableTreeNode e111;
DefaultMutableTreeNode e222;
DefaultMutableTreeNode e333;

DefaultMutableTreeNode aChild;
private JTree tree;
private JTextField currentSelectionField;

public SelectableTree() {
    super("JTree Selections");
    WindowUtilities.setNativeLookAndFeel();
    addWindowListener(new ExitListener());
    Container content = getContentPane();
    root = new DefaultMutableTreeNode("Root");

    e1 = new DefaultMutableTreeNode("1");
    e2 = new DefaultMutableTreeNode("2");
    e3 = new DefaultMutableTreeNode("3");

    e11 = new DefaultMutableTreeNode("1.1");
    e22 = new DefaultMutableTreeNode("2.2");
    e33 = new DefaultMutableTreeNode("3.3");

    e111 = new DefaultMutableTreeNode("1.1.1");
    e222 = new DefaultMutableTreeNode("2.2.2");
    e333 = new DefaultMutableTreeNode("3.3.3");

    root.add(e1);
    root.add(e2);
    root.add(e3);
    e1.add(e11);
    e2.add(e22);
    e3.add(e33);
    e11.add(e111);
    e22.add(e222);
    e33.add(e333);

    tree = new JTree(root);
    tree.addTreeSelectionListener(this);
    content.add(new JScrollPane(tree), BorderLayout.CENTER);
    currentSelectionField = new JTextField("Current Selection: NONE");
    content.add(currentSelectionField, BorderLayout.SOUTH);
    setSize(250, 275);
    setVisible(true);
}

public void valueChanged(TreeSelectionEvent event) {
    tree.clearSelection();
    tree.collapsePath(new TreePath(tree.getModel().getRoot()));
    tree.expandPath(new TreePath(e111.getPath()));
}

Edit: strangely enough the tree.collapsePath works just fine, it seems that tree.expandPath(new TreePath(e111.getPath())); is the problem

  • 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-06T19:44:04+00:00Added an answer on June 6, 2026 at 7:44 pm

    Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.JTree;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreePath;
    
    public class SelectableTree extends JFrame implements TreeSelectionListener {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new SelectableTree();
                }
            });
        }
        DefaultMutableTreeNode root;
        DefaultMutableTreeNode e1 = new DefaultMutableTreeNode("1");
        DefaultMutableTreeNode e2 = new DefaultMutableTreeNode("2");
        DefaultMutableTreeNode e3 = new DefaultMutableTreeNode("3");
        DefaultMutableTreeNode e11 = new DefaultMutableTreeNode("1.1");
        DefaultMutableTreeNode e22 = new DefaultMutableTreeNode("2.2");
        DefaultMutableTreeNode e33 = new DefaultMutableTreeNode("3.3");
        DefaultMutableTreeNode e111 = new DefaultMutableTreeNode("1.1.1");
        DefaultMutableTreeNode e222 = new DefaultMutableTreeNode("2.2.2");
        DefaultMutableTreeNode e333 = new DefaultMutableTreeNode("3.3.3");
        DefaultMutableTreeNode aChild;
        private JTree tree;
        private JTextField currentSelectionField;
    
        public SelectableTree() {
            super("JTree Selections");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            root = new DefaultMutableTreeNode("Root");
            root.add(e1);
            root.add(e2);
            root.add(e3);
            e1.add(e11);
            e2.add(e22);
            e3.add(e33);
            e11.add(e111);
            e22.add(e222);
            e33.add(e333);
    
            tree = new JTree(root);
            tree.addTreeSelectionListener(this);
            add(new JScrollPane(tree), BorderLayout.CENTER);
            currentSelectionField = new JTextField("Current Selection: NONE");
            add(currentSelectionField, BorderLayout.SOUTH);
            setSize(250, 275);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        @Override
        public void valueChanged(TreeSelectionEvent event) {
            tree.expandPath(new TreePath(e11.getPath()));
            currentSelectionField.setText(event.getPath().toString());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with show or hide form in Window Form Application. I start
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a problem with expanding/collapsing a Treeview control in an Updatepanel which causes
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with UIWebView delay when the load image from url. In my
I have problem while loading data into html select when users press or click
I have problem developing with live555. I already build the lib-files and example projects
I have problem with TableView its empty. In (.h) file: @interface TableViewController : UITableViewController{

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.