简体   繁体   English

AbstractTableModel DELETE行不起作用

[英]AbstractTableModel DELETE row not working

i have this table model, and i insert values calling in this way: 我有这个表模型,并且我插入以这种方式调用的值:

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

lista is the JTable lista是JTable

the Model: 该模型:

    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);  
}  

}

but the problem is, i CANT call the deleteLine().... 但问题是,我无法调用deleteLine()。

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

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

i dont know how to make this to work... since i cant access 我不知道如何使它工作...因为我无法访问

i think the mistake is that the Constructor is the one that "populate" the table, i think i have to change it and create a insertLine().. 我认为错误是构造函数是“填充”表的构造函数,我认为我必须更改它并创建insertLine()。

but not sure 但不确定

The problem is your not firing any kind of event from the model that would tell the table that the model has changed. 问题是您没有从模型中触发任何类型的事件来告诉表模型已更改。

Try using fireTableRowsDeleted after you have removed the rows from the underlying data structure within the model 从模型中的基础数据结构中删除行后,尝试使用fireTableRowsDeleted

Take a look at How to use tables and Firing Data Change Events in particular 看一看如何使用表 ,尤其是触发数据更改事件

Maybe would be that deleteLine() is a method that belongs to simpleTableModel , but you aren't creating any instance of that simpleTableModel on the frame. 可能是deleteLine()是属于simpleTableModel的方法,但是您不会在框架上创建该simpleTableModel任何实例。 You are doing directly 你在直接做

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

Try doing in your frame: 尝试在您的框架中做:

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

And inside your model something like this: 在模型内部,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM