从 JTreeTable SwingX 错误中删除所选节点
Remove selected node from JTreeTable SwingX error
当我试图从 JTreeTable 中删除选定的节点时,我遇到了一个奇怪的错误。
首先,我从 JXtreeTable
中选择了主 class 中的行
table.getSelectedRow()
我用这个方法做的,returns我得到了正确的行数。
但是后来,狗屎发生了。
在我的模型中 class 我调用方法 delete children:
package JTreeTable;
import org.jdesktop.swingx.treetable.AbstractMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
import org.jdesktop.swingx.treetable.MutableTreeTableNode;
import org.jdesktop.swingx.treetable.TreeTableModel;
public class TableModel extends DefaultTreeTableModel implements TreeTableModel {
DefaultMutableTreeTableNode root;// = new DefaultMutableTreeTableNode("Root");
public TableModel(DefaultMutableTreeTableNode root){
super(root);//it didnt work without this line. it took me a day to figure it out. I hate my life sometimes.
this.root = root;
}
public int getColumnCount()
{
return 4;
}
@Override
public String getColumnName( int column )
{
switch( column )
{
case 0: return "Description";
case 1: return "Amount";
case 2: return "Date";
case 3: return "Paid";
default: return "Unknown";
}
}
public void addChildren(){
int rootChildCount = root.getChildCount();
ArrayNode newChild = new ArrayNode(new Object[] {"dfds", "sfddsf","dfsfsd","fdsfsd"});
this.insertNodeInto(newChild, root, rootChildCount);
}
public void removeChildren(int index){
//root.remove(index);
System.out.println(root.getChildCount());
}
}
这是我的节点 class:
package JTreeTable;
import org.jdesktop.swingx.treetable.AbstractMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
public class ArrayNode extends DefaultMutableTreeTableNode {
public ArrayNode(Object[] data) {
super(data);
}
@Override
public Object getValueAt(int column) {
return getUserObject()[column];
}
@Override
public void setValueAt(Object aValue, int column) {
getUserObject()[column] = aValue;
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object[] getUserObject() {
return (Object[]) super.getUserObject();
}
@Override
public boolean isEditable(int column) {
return true;
}
}
所以,当我使用方法 root.remove(index) 时,我总是得到错误 "Node must be a valid node managed by this model"。但我很确定我的节点没问题!这是什么意思,是否可以修复?
您正试图绕过 TreeTableModel 直接从另一个节点移除子节点。使用 DefaultTreeTableModel.removeNodeFromParent,例如:
public void removeChildren(int index) {
MutableTreeTableNode node = root.getChildAt(index);
this.removeNodeFromParent(node);
}
当我试图从 JTreeTable 中删除选定的节点时,我遇到了一个奇怪的错误。 首先,我从 JXtreeTable
中选择了主 class 中的行table.getSelectedRow()
我用这个方法做的,returns我得到了正确的行数。 但是后来,狗屎发生了。 在我的模型中 class 我调用方法 delete children:
package JTreeTable;
import org.jdesktop.swingx.treetable.AbstractMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
import org.jdesktop.swingx.treetable.MutableTreeTableNode;
import org.jdesktop.swingx.treetable.TreeTableModel;
public class TableModel extends DefaultTreeTableModel implements TreeTableModel {
DefaultMutableTreeTableNode root;// = new DefaultMutableTreeTableNode("Root");
public TableModel(DefaultMutableTreeTableNode root){
super(root);//it didnt work without this line. it took me a day to figure it out. I hate my life sometimes.
this.root = root;
}
public int getColumnCount()
{
return 4;
}
@Override
public String getColumnName( int column )
{
switch( column )
{
case 0: return "Description";
case 1: return "Amount";
case 2: return "Date";
case 3: return "Paid";
default: return "Unknown";
}
}
public void addChildren(){
int rootChildCount = root.getChildCount();
ArrayNode newChild = new ArrayNode(new Object[] {"dfds", "sfddsf","dfsfsd","fdsfsd"});
this.insertNodeInto(newChild, root, rootChildCount);
}
public void removeChildren(int index){
//root.remove(index);
System.out.println(root.getChildCount());
}
}
这是我的节点 class:
package JTreeTable;
import org.jdesktop.swingx.treetable.AbstractMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
public class ArrayNode extends DefaultMutableTreeTableNode {
public ArrayNode(Object[] data) {
super(data);
}
@Override
public Object getValueAt(int column) {
return getUserObject()[column];
}
@Override
public void setValueAt(Object aValue, int column) {
getUserObject()[column] = aValue;
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object[] getUserObject() {
return (Object[]) super.getUserObject();
}
@Override
public boolean isEditable(int column) {
return true;
}
}
所以,当我使用方法 root.remove(index) 时,我总是得到错误 "Node must be a valid node managed by this model"。但我很确定我的节点没问题!这是什么意思,是否可以修复?
您正试图绕过 TreeTableModel 直接从另一个节点移除子节点。使用 DefaultTreeTableModel.removeNodeFromParent,例如:
public void removeChildren(int index) {
MutableTreeTableNode node = root.getChildAt(index);
this.removeNodeFromParent(node);
}