2014-01-16 85 views
0

我有這個表的模型,我插入值調用這種方式:AbstractTableModel上刪除行不工作

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

} 

但問題是,我CANT調用deleteLine()....

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

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

我不知道如何使這工作...因爲我無法訪問

我認爲的錯誤是,構造是一個「填充」表,我想我必須改變它,並創建一個insertLine()..

,但不知道

回答

1

看看How to use tablesFiring Data Change Events也許將是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; 
    } 
+0

完善,解決了這個問題完美;) – user2582318

+0

快樂幫:) – carexcer

3

的問題是你沒有發射任何類型的事件從模型會告訴該模型已經改變了表。

使用fireTableRowsDeleted嘗試已經取下了模型

內從底層數據結構中的行後採取特別