Skip to main content

Removing a selected node in JTree

11 replies [Last post]
avich
Offline
Joined: 2005-09-18
Points: 0

It seems that if a selected node is deleted from a tree.
The tree UI automatically selects the first node of the deleted node parent; at least it is rendered as a selected node.
But, the removed node still remains in the tree selected paths.

Why the tree doesn’t remove the deleted node from the tree selection model.

Should it be done explicitly by the tree implementation?

Currently the tree implementation only notifies the tree model listeners regarding the deleted node.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
leouser
Offline
Joined: 2005-12-12
Points: 0

at what point are you investigating the selection paths?

leouser

avich
Offline
Joined: 2005-09-18
Points: 0

The tree selected node is investigated right after a selected node is deleted.

The tree shows that another node gets selected, but the tree selected nodes still contains the deleted node path.

if the new selected node is explicitly reselected, then the tree selection model contains the correct state.

leouser
Offline
Joined: 2005-12-12
Points: 0

its possible that if you are doing it in a listener, as I suspect, the tree selection code has not had a chance to update itself yet. Which would explain why your seeing invalid data. That kind of problem can manifest itself in other types of listeners.

leouser

leouser
Offline
Joined: 2005-12-12
Points: 0

oh yeah, if you are doing it in a listener you may want to wrap the code in a Runnable and throw it into SwingUtilities.invokeLater. When you get the event toss of the Runnable and see what the selected paths are then. That is if you can wait. :D

leouser

avich
Offline
Joined: 2005-09-18
Points: 0

I'm not doing it in a listener.

It’s after the event of the deletion is completed and the tree node is visibly removed from the tree.

I have checked whether the one of the versions of TreeSelectionModel.removeSelectionPaths() is being called while a tree node is deleted, and its not.

I'm using treeModelLister.treeNodeRemoved().

It seems that I need to explicitly call TreeSelectionModel.removeSelectionPaths when I'm trying to delete a node, but it seems strange!.

leouser
Offline
Joined: 2005-12-12
Points: 0

weird, can you put up some simple code illustrating the problem...

leouser

avich
Offline
Joined: 2005-09-18
Points: 0

following Following is a test program that illustrates the problem.

In order to reproduce the problem, please follow the following steps:

1. Run the program.
2. Select a single non root node.
3. Click the "remove selected" button.
4. The selected node has been removed.
5. Click again the "remove selected" button.
6. The tree.getSelectionPath() should return a null selection
And the event should exit silently, but it returns the previously selected path thus throwing a NullPointerException.

-----CODE START-----------------
import java.awt.*;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.tree.*;

public class Test extends JFrame {
public Test() {
super("TreeCase");
setLayout(new FlowLayout());
DefaultMutableTreeNode root = new DefaultMutableTreeNode
("root");
DefaultTreeModel model = new DefaultTreeModel(root);
final JTree tree = new JTree(model);
getContentPane().add(tree);

setSize(new Dimension(608, 384));

DefaultMutableTreeNode node1 = new
DefaultMutableTreeNode("node1");
root.add(node1);
model.nodesWereInserted(root, new int[]{0});
tree.expandRow(0);

DefaultMutableTreeNode node1a = new
DefaultMutableTreeNode("node1a");
node1.add(node1a);
model.nodesWereInserted(node1, new int[]{0});
tree.expandRow(1);

DefaultMutableTreeNode node2 = new
DefaultMutableTreeNode("node2");
root.add(node2);
model.nodesWereInserted(root, new int[]{1});

JButton button = new JButton("remove selected");
getContentPane().add(button);

button.addActionListener(new ActionListener()
{

public void actionPerformed(java.awt.event.ActionEvent e) {

TreePath path = tree.getSelectionPath();
if (path!=null)
{

MutableTreeNode node =(MutableTreeNode) path.getLastPathComponent();

System.out.println("Trying to remove : "+node.toString());

MutableTreeNode parent=(MutableTreeNode)node.getParent();
int index=parent.getIndex(node);
parent.remove(node);

DefaultTreeModel model=(DefaultTreeModel)tree.getModel();
model.nodesWereRemoved(parent,new int[]{index},null);

}

}
});

setVisible(true);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run() { new Test();}
}
);
}
}
-----CODE ENDS-----------------

leouser
Offline
Joined: 2005-12-12
Points: 0

thanks for the test case. Ill look at it today sometime today. If there is a bug we may need to open a bug report on it. Then maybe Ill do a collab patch for it. Will report later.

leouser

leouser
Offline
Joined: 2005-12-12
Points: 0

I believe your problem is because:
model.nodesWereRemoved(parent, new int[]{index}, null);

must be expressed differntly, like so:
model.nodesWereRemoved(parent, new int[]{index}, new Object[]{node});

not passing an array of children that were removed results in the bug. Adding the array takes it away.

leouser

avich
Offline
Joined: 2005-09-18
Points: 0

Yes, it was the problem.

I always thought that sending the children is redundent.

Thanks a lot

tengfeia
Offline
Joined: 2013-10-04
Points: 0

商"は正規"が届きましたが、納期が遅れたので心配しました。納期予定日の8日後'過ぎても商"が届かず、連絡もなかったので"ちらからメールで確認しましたが、翌日になっても"信がなく、そう"うしているうちに商"が届きました。結果届いたので良かったですが、届かないのなら、他で注文しようかと思っていたのでそわそわしました。""で購入される場合は、急いでない場合は問題ないかと思います。

UGGブーツ
UGGブーツ 2013 新作
UGGブーツ 最安通販
UGGブーツ
UGG キッズ ブーツ
UGG メンズ
UGG ブーツ
UGG ブーツ 人気
UGG ブーツ コストコ