Add line with empty fields in a Jtable

Asked

Viewed 628 times

1

I have a AbstractTableModel only that I do not know how to add blank lines, when pressing the add line button on my frame I want it to call a method called addLine() of my Modelotabela, someone can 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...

    }

}

1 answer

0


Just add a Object empty in the method addLinha(), and inform listeners that the list has been modified, using fireTableDataChanged():

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

In his TableModel the method setValueAt() 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 of how to create a TableModel and use it to popular a table.

How popular a Jtable with its own Tablemodel?

  • I had already tried so, not from error, but also does nothing, I saw some examples that made addLine() with something inside the ()

  • @Matheuslopes you said add blank line. To add a line to a Jtable, you must add an Index to your list with an object, either empty or null. The above Code has been tested and adds an unfilled line. If that’s not what you were trying to do, try to edit the question and be clearer on what you need to do.

  • Ue, that’s exactly what I want... could you pass me your test code for me to check?

  • On my frame button I am trying to call addLine() as follows: Modelotabela2 tab = new Modelotabela2(); tab.addLine(); is this what is wrong?

  • You have set the model in the table?

  • Yes, is there any way I can send you the code from my frame? I’m a beginner in jTables, having a hard time with simple things...

  • @Matheuslopes see the answer edition and follow the link I added, there I explain step-by-step how to create a tablemodel.

  • Wow, now I’ve been floating a lot, I have no idea how I do setValueAt adapting to my code, it seems so simple, but I don’t know how to run, I need to study more about tablemodel, but is there any way to give me a solution for the moment? thanks for the help

  • @Matheuslopes you read the answer there from the link? It is very well explained there. And that’s another question. Take it easy on the answer, close this question like you accept. If even after reading there you cannot implement, ask another question about setvalueAt.

  • Okay, thanks, I’ll try here

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.