Allow only integers in a Jtable column

Asked

Viewed 422 times

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();
}

3 answers

1


The correct way to restrict this is through the method getColumnClass that your Tablemodel inherits from AbstractTableModel:

public Class<?> getColumnClass(int columnIndex) { 
    return columnIdex == 3 ? Integer.class : super.getColumnClass(columnIndex);
}

That way there will not allow to save any value that is not of the type Integer in this column it is necessary to either leave blank or fill in correctly to allow the completion of the cell edition in this column.

Just to illustrate what will happen, see the gif below:

inserir a descrição da imagem aqui

0

Good morning. As commented Articuno, I had to change to not allow text or return zero, although it is very useful your suggestion. Thanks for the suggestions. Just to finish I’ll put the final chunk of the tablemodel. I pass the value to another method, validates and returns.

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    Object[] linha = (Object[]) getLinhas().get(rowIndex);
    int valor = validateInt(aValue);
    linha[columnIndex] = valor;
    fireTableDataChanged();
}

private int validateInt(Object numberObj){
    try {
        int number = Integer.parseInt((String) numberObj);
        return number; 
    } catch (Exception e) {
        return 0;
}
}
  • I think you can make it simple: linha[columnIndex] = validateInt(aValue);

  • That’s right. rsrs.. Thank you

  • I do not understand, if you did so, getcolumnclass is not necessary.

  • Alias, the method of getColumnClass will not pass anything that can not be converted to integer for the setValueAt. You didn’t use this method?

  • 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.

  • 1

    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.

  • 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.

  • 1

    Always present a [mcve], thus, it avoids receiving answers that are incomplete or that do not meet your problem.

Show 3 more comments

-1

Using cast of Int you can do this validation:

private int validateInt(float number){
if(number == (int)number)
   return number;
else
   return 0
}
  • And how will he apply this on the table? Also do not apply to jtable, will still burst exception.

  • He asked for a function to validate input of integer numbers friend

  • 1

    Integer input in a jtable, So that method won’t work, not like this. The correct way is how I responded, using the model class itself that will monitor data input according to type. Another thing, the table row allows you to type anything, if I type a string, that your way ai will lock the application all popping an exception.

  • 1

    Thank you for your cooperation. What I really wanted was what Articuno suggested to me. Validate whole, in column 3 and in a jTable. I just completed the code here and it was as expected. Thank you

  • @rafB the problem is that this method will surely burst exception if you apply in your Model, and will lock the table, so I questioned the author. In text mode, it might be functional, for Jtable this method would not work.

Browser other questions tagged

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