KarolisShp KarolisShp - 1 month ago 22
Java Question

Java JTable specific row and column value + another integer

I tried adding up a specific JTable cell value (in this case at (y,2)) with a value of another integer.

The idea is that you add up the initial value in the cell (which you convert to integer) with the value from JTextField (which you convert to integer)

int value = Integer.parseInt(table.getModel().getValueAt(y, 2)) + Integer.parseInt(materialsquantities[x].getText());


This line throws a problem. I guess this has something to do with the type of the cell value which should be a string (?).

Answer

JTable could contain any type of objects (e.g. String, Integers, etc), so getValueAt returns Object, not a String. You should either cast result of getValueAt to String (if the JTable contains only Strings) or explicitly convert Object to String, i.e. use toString() method. In both cases you should remember about possible null values.