0

is it possible for the cell to be input with integer only? This is my code.

@Override
public boolean isCellEditable(int row, int col) {
    return col == 3;
}

@Override
public void setValueAt(Object value, int row, int col) {
    data[row][col] = value;
    fireTableCellUpdated(row, col);
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Ng Wei Ze
  • 1
  • 4
  • 1
    I don't understand the question; could you please rephrase it? What is the type of `data`? – Codor Jan 28 '15 at 12:31
  • You can add a validation at the begging of the `setValueAt` method. – Salah Jan 28 '15 at 12:31
  • The question is missing context. It seems you try to inherit from some GUI-Table-class, maybe JTable or implementing maybe TableModel? Without this context nobody can help you, as we all can only guess what's the problem. – Mnementh Jan 28 '15 at 12:35
  • Create a model to supply your table with. See -> [The Java™ Tutorials: Creating a Table Model](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data) – Mr. Polywhirl Jan 28 '15 at 12:35

3 Answers3

0

Yes. Use instanceof to check if object is of type Integer. (Usage Example)

If String is going to be sent and you want to check if it is integer, use

Integer.parseInt((String)value)

It throws an exception when the value is not parsable or value is not of type String. You can implement your control accordingly.

Community
  • 1
  • 1
Seyf
  • 889
  • 8
  • 16
0

Simple solution:

@Override
public void setValueAt(Object value, int row, int col){

    if(!isIntegerValue(value)) {
        throw new Exceoption("not integer value");
    }

    data[row][col]=value;
    fireTableCellUpdated(row, col);
}

private boolean isIntegerValue(Object value) {
    return value instanceof Integer;
}
Salah
  • 8,567
  • 3
  • 26
  • 43
  • Thanks for your solution. However, when i type integer values, there is an error. It says :java.lang.Exception: Not integer value – Ng Wei Ze Jan 28 '15 at 12:51
0

Check the type of value (value instanceof Integer) and throw an Exception in case it is not integer. Unless you do not want to use some kind of wrapper, there is no chance to ensure the type of value at compile time, while you have to keep the method's header.

jp-jee
  • 1,502
  • 3
  • 16
  • 21