user3690926 user3690926 - 8 months ago 50
Java Question

How to add functions on columns of table in Java?

I am new in programming and I am looking for a way to add functions on columns of table in java just like MS Excel.

Answer Source

You'll need to implement your own DefaultTableModel or AbstractTableModel to be able to output your own values in the third column. Another user has posted an example here which demonstrates how to multiply the first two columns together and output the result in the third column. In the example, you can double click the JTable to add another row and you will notice that the results are always multiplied and shown in the third column. These calculations are performed in the custom table model.

In regards to a parser in the cells, I imagine you would have to override the getValueAt(int row, int column) function and check if the value at the given row and column follows the function syntax (ie: in MS Excel, functions always start with an equal sign). If the value does follow the syntax, you can strip the relevant details and then perform the necessary calculations.