I am having a strange error when I am trying to remove selected node from JTreeTable. First of all, I get selected row in my main class from JXtreeTable
table.getSelectedRow()
I do it with this method, and it returns me a correct number of row. But then, shit happends. In my model class I call method 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());
}
}
And here my node 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;
}
}
So, when i use method root.remove(index) i always get error "Node must be a valid node managed by this model". But i am pretty sure my nodes are ok! What this means and is it fixable?
You are trying to remove the child directly from another node, bypassing the TreeTableModel. Use DefaultTreeTableModel.removeNodeFromParent, e.g:
public void removeChildren(int index) {
MutableTreeTableNode node = root.getChildAt(index);
this.removeNodeFromParent(node);
}