Olá, estou desenvolvendo o meu próprio AbstractTableModel, necessito que a tabela seja criada conforme o usuário clique e que ele possa editar todas as células da tabela que esta criando, estou com problemas no setValueAt(), quando escrevo e saio do campo o mesmo volta a ficar em branco;
Segue código:
public class AbstractTable extends AbstractTableModel {
private List<String> colunas = new ArrayList<String>();
private List<List<String>> linhas = new ArrayList<List<String>>();
private List<String> conteudo = new ArrayList<String>();
public AbstractTable(List<String> titulo, List<String> conteudo) {
this.colunas = titulo;
this.conteudo = conteudo;
}
public int getRowCount() {
return linhas.size();
}
public List<List<String>> getRow(){
return this.linhas;
}
public List<String> getColumn(){
return this.colunas;
}
public int getColumnCount() {
return colunas.size();
}
public Object getValueAt(int linha, int coluna) {
return getRow().get(linha).get(coluna);
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//this.linhas.set(rowIndex, (List<String>) aValue);
//fireTableDataChanged();
fireTableCellUpdated(rowIndex, columnIndex);
//fireTableStructureChanged();
}
public void addRow(List<String> conteudo){
if (conteudo.size() != colunas.size()){
throw new IllegalArgumentException("conteudo diferente da quantidade de colunas.");
}
ArrayList<String> linha = new ArrayList<String>();
for(String valor: conteudo){
linha.add(valor);
}
linhas.add(linha);
fireTableDataChanged();
}
public void addColumn(String titulo, String conteudo){
this.colunas.add(titulo);
this.conteudo.add(conteudo);
for (List<String> linha : this.linhas) {
linha.add(conteudo);
}
fireTableStructureChanged();
}
public void removeRow(int linha){
getRow().remove(linha);
fireTableStructureChanged();
}
public void removeColumn(int coluna){
getColumn().remove(coluna);
fireTableStructureChanged();
}
// Todas vão ser editáveis, então retorna true pra todas.
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
}
Classe de teste:
public class Main extends JFrame {
private JTable tabela;
private JScrollPane scrollPane;
private JButton buttonAdicionarLinha, buttonRemoverLinha, buttonAdicionarColuna, buttonRemoverColuna;
private ArrayList<String> titulo, conteudo;
private AbstractTable modeloTela;
private int quantidadeLinhas = 0;
public Main(){
this.setTitle("Criar Tabela");
this.setSize(500, 310);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setResizable(false);
this.titulo = new ArrayList<String>();
this.conteudo = new ArrayList<String>();
this.modeloTela = new AbstractTable(this.titulo, this.conteudo);
this.tabela = new JTable(modeloTela);
this.tabela.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.scrollPane = new JScrollPane(tabela);
this.scrollPane.setBounds(10, 10, 475, 200);
this.add(this.scrollPane);
this.buttonAdicionarLinha = new JButton("Adicionar Linha");
this.buttonAdicionarLinha.setBounds(10, 215, 130, 25);
this.buttonAdicionarLinha.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (titulo.size() == 0)
modeloTela.addColumn("", "");
ArrayList<String> linha = new ArrayList<String>();
for(int coluna = 0; coluna < titulo.size(); coluna++){
//if(conteudo.get(coluna).equals(String.class)){
linha.add(titulo.get(coluna));
//}else if(conteudo.get(coluna).equals(Integer.class)){
// linha.add("1");
//}
}
modeloTela.addRow(conteudo);
quantidadeLinhas++;
}
});
this.add(this.buttonAdicionarLinha);
this.buttonRemoverLinha = new JButton("Remover Linha");
this.buttonRemoverLinha.setBounds(10, 245, 130, 25);
this.buttonRemoverLinha.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tabela.getSelectedRow() > -1) {
modeloTela.removeRow(tabela.getSelectedRow());
quantidadeLinhas--;
if (quantidadeLinhas == 0) {
for (int i = titulo.size() -1; i > -1; i--) {
modeloTela.removeColumn(i);
}
titulo.removeAll(titulo);
conteudo.removeAll(conteudo);
}
}
}
});
this.add(this.buttonRemoverLinha);
this.buttonAdicionarColuna = new JButton("Adicionar Coluna");
this.buttonAdicionarColuna.setBounds(355, 215, 130, 25);
this.buttonAdicionarColuna.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
modeloTela.addColumn("", "");
}
});
this.add(this.buttonAdicionarColuna);
this.buttonRemoverColuna = new JButton("Remover Coluna");
this.buttonRemoverColuna.setBounds(355, 245, 130, 25);
this.buttonRemoverColuna.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int coluna = tabela.getSelectedColumn();
if (coluna > -1) {
conteudo.remove(coluna);
modeloTela.removeColumn(coluna);
if (titulo.size() == 0){
for (int i = quantidadeLinhas -1; i > -1; i--) {
modeloTela.removeRow(i);
}
quantidadeLinhas = 0;
modeloTela.fireTableStructureChanged();
}
}
}
});
this.add(this.buttonRemoverColuna);
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}