0
Below I have a model of Jtable
. There is only one editable column for number (seconds). How to validate to accept only integers, and in case the user enters anything else except integer, return ZERO?
public class Tabela_Fluxograma extends AbstractTableModel {
private ArrayList linhas = null;
private String[] colunas = null;
public Tabela_Fluxograma (ArrayList lin, String[] col){
setLinhas(lin);
setColunas(col);
}
public ArrayList getLinhas() {
return linhas;
}
public void setLinhas(ArrayList dados) {
this.linhas = dados;
}
public String[] getColunas() {
return colunas;
}
public void setColunas(String[] nome) {
this.colunas = nome;
}
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];
}
@Override
public boolean isCellEditable(int row, int column) {
return column == 3;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object[] linha = (Object[]) getLinhas().get(rowIndex);
linha[columnIndex] = aValue;
//este método é quem notifica a mudança do model na tabela
fireTableDataChanged();
}
I think you can make it simple:
linha[columnIndex] = validateInt(aValue);
– user28595
That’s right. rsrs.. Thank you
– rafB
I do not understand, if you did so, getcolumnclass is not necessary.
– user28595
Alias, the method of
getColumnClass
will not pass anything that can not be converted to integer for thesetValueAt
. You didn’t use this method?– user28595
I did not use the method. I was not "productive" here the text display. It was necessary to reset immediately. The entered value goes straight to the database.
– rafB
How so productive? My suggestion does not let setvalueat be called until the implicit column type conversion is correct. So your question code isn’t complete. My suggestion was based on it, but if you do not present everything that is necessary in the question, it can happen this.
– user28595
So, it was a mistake of mine. Since I’m a beginner in java I thought it would not be necessary to present. But it was also useful for another table model that I have. The two suggestions helped me.
– rafB
Always present a [mcve], thus, it avoids receiving answers that are incomplete or that do not meet your problem.
– user28595