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...
}
}
I had already tried so, not from error, but also does nothing, I saw some examples that made addLine() with something inside the ()
– Matheus Lopes
@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.
– user28595
Ue, that’s exactly what I want... could you pass me your test code for me to check?
– Matheus Lopes
On my frame button I am trying to call addLine() as follows: Modelotabela2 tab = new Modelotabela2(); tab.addLine(); is this what is wrong?
– Matheus Lopes
You have set the model in the table?
– user28595
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...
– Matheus Lopes
@Matheuslopes see the answer edition and follow the link I added, there I explain step-by-step how to create a tablemodel.
– user28595
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
– Matheus Lopes
@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.
– user28595
Okay, thanks, I’ll try here
– Matheus Lopes