JTree Selection lost when JTree looses focus

J

jumpz

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?
 
J

jumpz

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.
 
J

jumpz

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.

I figured it out. I put JTree.getSelectionPaths() and
JTree.setSelectionPaths() around reload()
 
J

Jean-Baptiste Nizet

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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top