AbstractTableModel DELETE行无效

时间:2014-01-16 19:58:14

标签: java swing jtable tablemodel

我有这个表模型,我插入以这种方式调用的值:

lista.setModel(new SimpleTableModel(dados, colunas));

lista是JTable

模特:

    import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

public class SimpleTableModel extends AbstractTableModel{  

    private ArrayList linhas = null;  
    private String [] colunas = null;  
    public String[] getColunas() {return colunas;}  
    public ArrayList getLinhas() {return linhas;}  
    public void setColunas(String[] strings) {colunas = strings;}  
    public void setLinhas(ArrayList list) {linhas = list;}
    @Override
    /** 
     * Retorna o numero de colunas no modelo 
     * @see javax.swing.table.TableModel#getColumnCount() 
     */  
    public int getColumnCount() {return getColunas().length;}  

    /** 
     * Retorna o numero de linhas existentes no modelo 
     * @see javax.swing.table.TableModel#getRowCount() 
     */  
    public String getColumnName(int col){  
        return getColunas()[col];  
    }  

    public int getRowCount() {return getLinhas().size();}  

    /** 
     * Obtem o valor na linha e coluna 
     * @see javax.swing.table.TableModel#getValueAt(int, int) 
     */  
    public Object getValueAt(int rowIndex, int columnIndex) {  
        // Obtem a linha, que é uma String []  

        String [] linha = (String [])getLinhas().get(rowIndex); 

        // Retorna o objeto que esta na coluna  
        return linha[columnIndex];  
    }  
//added this to try...
    public boolean deleteLine(int line){   
        linhas.remove(line);   
        return true;   
    }   

    public boolean deleteAll(){   
        linhas.clear();   
        return true;   
    }   


public SimpleTableModel(ArrayList dados, String[] colunas){  
    setLinhas(dados);  
    setColunas(colunas);  
}  

}

但问题是,我可以调用deleteLine()....

    public boolean deleteLine(int line){   
        linhas.remove(line);   
        return true;   
    }   

    public boolean deleteAll(){   
        linhas.clear();   
        return true;   
    }   

我不知道如何让它工作......因为我无法访问

我认为错误是构造函数是“填充”表,我想我必须更改它并创建一个insertLine()..

但不确定

2 个答案:

答案 0 :(得分:3)

问题是你没有从模型中触发任何类型的事件,告诉表格模型已经改变。

从模型

中的基础数据结构中删除行后,尝试使用fireTableRowsDeleted

特别关注How to use tablesFiring Data Change Events

答案 1 :(得分:1)

可能deleteLine()是属于simpleTableModel的方法,但您不是在框架上创建simpleTableModel的任何实例。你是直接做的

lista.setModel(new SimpleTableModel(dados, colunas));

尝试在你的框架中进行:

SimpleTableModel modelo = new SimpleTableModel(dados, colunas);
lista.setModel(modelo);
modelo.deleteX(line);

在你的模型里面是这样的:

public boolean deleteX(int line) throws DAOExcepcion{ //could be void because always returns true
        this.removeRow(line);
        this.fireTableRowsDeleted(line, line);
        return true;
    }