Add row with empty fields in a JTable

1

I have a AbstractTableModel only that I do not know how to add blank lines, when I hit the add line button in my frame I want it to call a method called addLine () from my TableTable, can anyone help me?

    public class ModeloTabela2 extends AbstractTableModel {
    private ArrayList linhas = new ArrayList();
    private String[] colunas = {"DATA", "EVOLUÇÃO"};

    public boolean isCellEditable(int linha, int coluna) {
        return true;
    }

    public ArrayList getLinhas(){
        return linhas;
    }
    public void setLinhas(ArrayList dados){
        linhas = dados;
    }
    public String[] getColunas(){
        return colunas;
    }
    public void setColunas(String[] nomes){
        colunas = nomes;
    }
    public int getColumnCount(){
        return colunas.length;
    }
    public int getRowCount(){
        return linhas.size();
    }
    public String getColumnName(int numCol){
        return colunas[numCol];
    }
    public Object getValueAt(int numLin, int numCol){
        Object[] linha = (Object[])getLinhas().get(numLin);
        return linha[numCol];
    }
    public Class<?> getColumnClass(int coluna) {
        switch(coluna){
        case 0:
            return String.class;
        case 1:
            return String.class;
        default:
            return String.class;
        }

    }
    public void addLinha(){       //Aqui eu adicionaria a linha em branco...

    }

}
    
asked by anonymous 29.04.2016 / 14:53

1 answer

0

Just add an empty% void to the Object method, and inform listeners that the list has been modified, using addLinha() :

public void addLinha(){ 
    this.linhas.add(new Object[]{});
    this.fireTableDataChanged();
}

On your fireTableDataChanged() method TableModel " is not defined, and it is precisely this method that tells your model how to add new objects (rows) in your table. See the question below for a brief explanation on how to create a setValueAt() and use it to populate a table.

How do I add a JTable with my own TableModel?

    
29.04.2016 / 15:12