将行插入自定义表模型的问题

时间:2011-04-06 12:10:52

标签: java jtable

我有这样的自定义表格模型。

class GrvTableModel extends AbstractTableModel {

public String[] columnNames = ["Key" , "Value" ]
public Object[][] data = []

public selectedObjArray

    //Returns the number of columns in the datamodel
    public int getColumnCount() {
        return columnNames.length;
    }

    //Returns the number of rows in the datamodel
    public int getRowCount() {
        return data.length;
    }

    //Returns the name of column in the datamodel at the specified index
    public String getColumnName(int col) {
        return columnNames[col];
    }

    //Returns the name of column in the datamodel at the specified index
    def String getColumn(int colIndx) {
        return super.getColumn(colIndx)
    }
    //Returns the object at the specified [row,column] index
    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

     public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col == 1) {
            return true;
        } else {
            return false;
        }
     }

//sets the object at the row and column as specified
    public void setValueAt(Object value, int row, int col) {
//        println "change table row: " + row

        data[row][col] = value;
        fireTableCellUpdated(row, col);
        selectedObjArray[row].val = value
    }

}

我需要在弹出的Popupmenu中单击“在下面插入行”菜单项后,在表格的选定行下方插入一个空行。为实现这一目标,我做了以下几点。

class TablePopupImpl extends MouseAdapter implements ActionListener{
JPopupMenu tablePopup;
JMenuItem deleteCells,insertRowBelow;
public TablePopupImpl() {
tablePopup = new JPopupMenu()
deleteCells = new JMenuItem("Delete Cells")
insertRowBelow = new JMenuItem("Insert Rows below")
tablePopup.add(deleteCells)
tablePopup.add(insertRowBelow)
insertRowBelow.addActionListener(this)
}
public void mousePressed(MouseEvent e) {
    if (e.isPopupTrigger()) {
            tablePopup.show((Component)e.getSource(), e.getX(), e.getY());
    }
}
public void mouseReleased(MouseEvent e) {
    if (e.isPopupTrigger()) {
            tablePopup.show((Component)e.getSource(),e.getX(), e.getY());
    }
}
public void actionPerformed(ActionEvent e) {
    Object src=e.getSource()
    if(src.equals(insertRowBelow)){
                 DefaultTreeModel obj = (DefaultTreeModel)table.getModel() //error here
             obj.insertRow(table.getSelectedRow()+1,null)           
    }
}

}

上面的行引发了一个错误,指出GrvtableModel即我的自定义表模型无法转换为默认表模型。

为了实现这一点,我必须用对自定义表模型的强制转换替换DefaultTreeModel的强制转换。

GrvTableModel obj = (GrvTableModel)table.getModel()

我必须在自定义表模型中编写一个方法,该方法可以在所选行下面添加一个空行。请告诉我如何编写这样的方法/如何实现我的要求?

1 个答案:

答案 0 :(得分:0)

如何使用List而不是数组?然后,您只需在指定的索引处插入一个新行。但是,您可能需要以某种方式重绘表格。

class GrvTableModel extends AbstractTableModel {

public String[] columnNames = ["Key" , "Value" ];
public List<Object[]> data = new ArrayList<Object[]>();

public selectedObjArray

    //Returns the number of columns in the datamodel
    public int getColumnCount() {
        return columnNames.length;
    }

    //Returns the number of rows in the datamodel
    public int getRowCount() {
        return data.size();
    }

    //Returns the name of column in the datamodel at the specified index
    public String getColumnName(int col) {
        return columnNames[col];
    }

    //Returns the name of column in the datamodel at the specified index
    def String getColumn(int colIndx) {
        return super.getColumn(colIndx)
    }
    //Returns the object at the specified [row,column] index
    public Object getValueAt(int row, int col) {
        return data.get(row)[col];
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

     public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col == 1) {
            return true;
        } else {
            return false;
        }
     }

//sets the object at the row and column as specified
    public void setValueAt(Object value, int row, int col) {
//        println "change table row: " + row

        data.get(row)[col] = value;
        fireTableCellUpdated(row, col);
        selectedObjArray[row].val = value
    }

    public void insertNewRow(Object[] values, int row) {
        data.add(row, values);
        // Trigger a table update / redraw somehow.
    }

}
相关问题