簡體   English   中英

從JTreeTable SwingX錯誤中刪除選定的節點

[英]Remove selected node from JTreeTable SwingX error

當我嘗試從JTreeTable中刪除選定的節點時遇到一個奇怪的錯誤。 首先,我從JXtreeTable的主類中選擇了一行

table.getSelectedRow()

我用這種方法來做,它返回正確的行數。 但是然后,發生了大便。 在我的模型類中,我調用方法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());   

    }

}

這是我的節點類:

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)時,總是出現錯誤“節點必須是由此模型管理的有效節點”。 但是我很確定我的節點還可以! 這意味着什么並且可以修復?

您試圖繞過TreeTableModel直接從另一個節點刪除該子級。 使用DefaultTreeTableModel.removeNodeFromParent,例如:

public void removeChildren(int index) {
    MutableTreeTableNode node = root.getChildAt(index);
    this.removeNodeFromParent(node);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM