Question

i have this table model, and i insert values calling in this way:

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

lista is the 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()....

    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()..

but not sure

Was it helpful?

Solution 2

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

OTHER TIPS

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

Take a look at How to use tables and Firing Data Change Events in particular

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top