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 / General / November 2007

Tip: Looking for answers? Try searching our database.

JTree Selection lost when JTree looses focus

Thread view: 
jumpz - 12 Nov 2007 08:43 GMT
Is there a way to prevent a JTree from loosing it's selection when it
looses focus?  Say I have a few items selected in the JTree and then I
go to press a button, that normally causes the JTree to unselect
everything, how can I stop this?
jumpz - 12 Nov 2007 09:24 GMT
> Is there a way to prevent a JTree from loosing it's selection when it
> looses focus?  Say I have a few items selected in the JTree and then I
> go to press a button, that normally causes the JTree to unselect
> everything, how can I stop this?

Guess what I'm really asking is: does DefaultTreeModel.reload() reset
the JTree selection, and if it does how can I prevent this.  Assume
that between DefaultTreeModel.reload()'s the only thing that changes
to the tree nodes is the userObject, not the number of nodes in the
tree.
jumpz - 12 Nov 2007 09:32 GMT
> > Is there a way to prevent a JTree from loosing it's selection when it
> > looses focus?  Say I have a few items selected in the JTree and then I
[quoted text clipped - 6 lines]
> to the tree nodes is the userObject, not the number of nodes in the
> tree.

I figured it out.  I put JTree.getSelectionPaths() and
JTree.setSelectionPaths() around reload()
Jean-Baptiste Nizet - 12 Nov 2007 15:55 GMT
> Is there a way to prevent a JTree from loosing it's selection when it
> looses focus?  Say I have a few items selected in the JTree and then I
> go to press a button, that normally causes the JTree to unselect
> everything, how can I stop this?

I don't have this behaviour on my machine (Windows XP, Java 1.5.0_10).
When I run the following program, select some tree nodes and press the
button at the bottom, it prints out the selected paths as it should.
Maybe it's your code that has a problem, but you don't show us
anything.

JB.

package foo;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class TreeTest extends JFrame {

 public TreeTest() {
   super("Tree test");
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
   root.add(new DefaultMutableTreeNode("child1"));
   root.add(new DefaultMutableTreeNode("child2"));
   root.add(new DefaultMutableTreeNode("child3"));
   TreeModel treeModel = new DefaultTreeModel(root);
   final JTree tree = new JTree(treeModel);
   getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
   JButton button = new JButton("Print selection");
   button.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       TreePath[] selectedPaths =
tree.getSelectionModel().getSelectionPaths();
       if (selectedPaths != null) {
         for (TreePath tp : selectedPaths) {
           System.out.println(tp);
         }
       }
     }
   });
   getContentPane().add(button, BorderLayout.SOUTH);
   pack();
 }

 public static void main(String[] args) {
   TreeTest t = new TreeTest();
   t.setVisible(true);
 }
}


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



©2009 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.