gpt4 book ai didi

java - AbstractTableModel 删除行不起作用

转载 作者:行者123 更新时间:2023-12-01 18:37:42 26 4
gpt4 key购买 nike

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

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

但不确定

最佳答案

问题是您没有从模型中触发任何类型的事件来告诉表模型已更改。

尝试使用fireTableRowsDeleted从模型内的基础数据结构中删除行后

看看How to use tablesFiring Data Change Events特别是

关于java - AbstractTableModel 删除行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21171609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com