JTree Selection lost when JTree looses focus

Discussion in 'Java' started by jumpz, Nov 12, 2007.

  1. jumpz

    jumpz Guest

    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, Nov 12, 2007
    #1
    1. Advertising

  2. jumpz

    jumpz Guest

    On Nov 12, 1:43 am, jumpz <> wrote:
    > 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, Nov 12, 2007
    #2
    1. Advertising

  3. jumpz

    jumpz Guest

    On Nov 12, 2:24 am, jumpz <> wrote:
    > On Nov 12, 1:43 am, jumpz <> wrote:
    >
    > > 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.


    I figured it out. I put JTree.getSelectionPaths() and
    JTree.setSelectionPaths() around reload()
    jumpz, Nov 12, 2007
    #3
  4. On 12 nov, 09:43, jumpz <> wrote:
    > 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);
    }
    }
    Jean-Baptiste Nizet, Nov 12, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    6,048
    Vova Reznik
    May 4, 2006
  2. =?ISO-8859-1?Q?J=F8rgen_Hansen?=

    Tkinter Listbox looses selection on Tab

    =?ISO-8859-1?Q?J=F8rgen_Hansen?=, Jul 22, 2003, in forum: Python
    Replies:
    2
    Views:
    468
    =?ISO-8859-1?Q?J=F8rgen_Cederberg?=
    Jul 24, 2003
  3. MarsVoyager
    Replies:
    3
    Views:
    118
  4. Gruupy
    Replies:
    0
    Views:
    278
    Gruupy
    Aug 15, 2006
  5. Roger
    Replies:
    3
    Views:
    303
Loading...

Share This Page