Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2006

Tip: Looking for answers? Try searching our database.

How to move focus from one JTree node to another one and then back again?

Thread view: 
romayankin@gmail.com - 15 Sep 2006 16:40 GMT
Hi all!
Say I have a very simple JTree

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package main;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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;

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

   private JTree tree;

   private JTextField currentSelectionField;

   public SelectableTree() {
       super("JTree Selections");
       addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent event) {
               System.exit(0);
           }
       });
       Container content = getContentPane();
       DefaultMutableTreeNode root = new
DefaultMutableTreeNode("Root");
       DefaultMutableTreeNode child;
       DefaultMutableTreeNode grandChild;
       for (int childIndex = 1; childIndex < 4; childIndex++) {
           child = new DefaultMutableTreeNode("Child " + childIndex);
           root.add(child);
           for (int grandChildIndex = 1; grandChildIndex < 4;
grandChildIndex++) {
               grandChild = new DefaultMutableTreeNode("Grandchild "
                       + childIndex + "." + grandChildIndex);
               child.add(grandChild);
           }
       }
       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) {
       currentSelectionField.setText("Current Selection: "
               + tree.getLastSelectedPathComponent().toString());
   }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All I need is to move focus from currently selected node, to some other
node (does not matter which one), then to "sleep" for a second and
finally move it back to return selection to the previously selected
node. The only question is how do I do this?
danharrisandrews@gmail.com - 16 Sep 2006 22:45 GMT
> All I need is to move focus from currently selected node, to some other
> node (does not matter which one), then to "sleep" for a second and
> finally move it back to return selection to the previously selected
> node. The only question is how do I do this?

Hi Roman Yankin,

Give this a try and see if it is what you wanted.

Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - -

package main;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

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

   private JTree tree;

   private JTextField currentSelectionField;

   private boolean isAdjusting = false;

   public SelectableTree() {
       super("JTree Selections");
       addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent event) {
               System.exit(0); }});
       Container content = getContentPane();
       DefaultMutableTreeNode root = new
DefaultMutableTreeNode("Root");
       DefaultMutableTreeNode child;
       DefaultMutableTreeNode grandChild;
       for (int childIndex = 1; childIndex < 4; childIndex++) {
           child = new DefaultMutableTreeNode("Child " + childIndex);
           root.add(child);
           for (int grandChildIndex = 1; grandChildIndex < 4;
grandChildIndex++) {
               grandChild = new DefaultMutableTreeNode("Grandchild "
                       + childIndex + "." + grandChildIndex);
               child.add(grandChild);
           }
       }
       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) {
       if (tree.getLastSelectedPathComponent() != null) {
           currentSelectionField.setText("Current Selection: "
               + tree.getLastSelectedPathComponent().toString());
           if (!isAdjusting) {
               changeSelection();
           }
       }
   }

   private void changeSelection() {
       TreeSelectionModel sm = tree.getSelectionModel();
       TreePath[] originalPaths = sm.getSelectionPaths();
       if (originalPaths.length > 0) {
           Object[] path = originalPaths[0].getPath();
           if (path.length > 0) {
               selectRandomPath(originalPaths);
           }
       }
   }

   private TreePath getRandomPath(TreePath[] originalPaths) {
       final List<TreeNode> path = new ArrayList<TreeNode>();
       TreeNode currentNode = (TreeNode)
originalPaths[0].getPath()[0];
       while (currentNode != null) {
           path.add(currentNode);
           int childCount = currentNode.getChildCount();
           if (childCount > 0) {
               currentNode = currentNode.getChildAt((int)
(Math.random()
                       * (childCount - 1) + 0.5));
           } else {
               currentNode = null;
           }
       }
       TreePath treePath = new TreePath(path.toArray());
       if (treePath.equals(originalPaths)) {
           treePath = getRandomPath(originalPaths);
       }
       return treePath;
   }

   private void selectRandomPath(final TreePath[] originalPaths) {
       Thread thread = new Thread() {
           public void run() {
               try {
                   Thread.sleep(500);
                   SwingUtilities.invokeLater(new Runnable() {
                       public void run() {
                           TreeSelectionModel sm =
tree.getSelectionModel();
                           isAdjusting = true;
                           sm.clearSelection();

sm.addSelectionPath(getRandomPath(originalPaths));
                       }
                   });
                   Thread.sleep(1000);
                   SwingUtilities.invokeLater(new Runnable() {
                       public void run() {
                           TreeSelectionModel sm =
tree.getSelectionModel();
                           sm.clearSelection();
                           sm.addSelectionPath(originalPaths[0]);
                           isAdjusting = false;
                       }
                   });
               } catch (InterruptedException e) {
                       e.printStackTrace();
               }
           }
       };
       thread.start();
   }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.