0

I have a table with 7 columns and also some rows, I want to multiply cells in the table.
Say when someone enters a value In (row 0, column 0) and (row 0, column 3) the final answer will automatically appear in (row 0, column 6)

This is my code when some clicks on a cell.
It isn't working

Double a=Double.parseDouble ((String) jTable.getValueAt (0,0);
Double b=Double.parseDouble ((String) jTable.getValueAt (0,3);
Double c=a*b;
jTable.setValueAt(c,0,6);
fabian
  • 80,457
  • 12
  • 86
  • 114
Prince Asamoah
  • 135
  • 3
  • 11
  • 1
    The underlying model inherited from AbstractTableModel have to fire events when model is updated. Please post your custom model. – Aubin Nov 27 '18 at 19:16

1 Answers1

1

First of all if your columns contain numbers then you should store the Double value in the model, not a String.

So you need to override the getColumnClass(...) method of your TableModel to tell the table the type of data stored in the column so the table can use the appropriate renderer/editor.

Some thing like:

@Override
public Class getColumnClass(int column)
{
    switch (column)
    {
        case 0: return Double.class;
        case 3: return Double.class;
        case 6: return Double.class;
        default: return Object.class;
    }
}

You would also override the isCellEditable(...) method to prevent data in column 6 from being edited, since its value will always be the result of values found in columns 0 and 3.

when someone enters a value

Whenever data is changed in the table the TableModel is updated.

So one approach is to override the setValueAt(...) method of your TableModel to recalculate the value for column 6.

The basic logic would be:

@Override
public void setValueAt(Object value, int row, int column)
{
    super.setValueAt(value, row, column);

    if (column == 0 || or column == 3)
    {
        double column0 = getValueAt(row, 0);
        double column3 = getValueAt(row, 3);
        double result = column0 * column3;
        setValueAt(result, row, 6);
    }
}

Read the section from the Swing tutorial on How to Use Tables for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I get it...Am using NetBeans and don't know how to get this done..Am a beginner...and also am using mouseClicked event...Thanks for this though – Prince Asamoah Nov 27 '18 at 20:59
  • @PrinceAsamoah, Which is why I always suggest you don't use an IDE to generated code for you. Don't be restricted by your knowledge of the IDE. Just use the IDE the help compile/debug etec. That way you learn to code in Java and if you ever move to a different IDE you don't need to relearn the IDE. – camickr Nov 27 '18 at 21:10
  • For an alternative approach you can add a `TableModelListener` to the `TableModel`. You should be able to do this in any IDE. Take a look at: https://stackoverflow.com/questions/3540661/tablemodellistener-and-multiple-column-validation/3541876#3541876 for a working example of this approach. However, this solution isn't as good as the first suggestion because now the "business logic" of your TableModel is in two different places. – camickr Nov 27 '18 at 21:12