Reputation: 849
I have a JTable witch implements TableModelListener, I have implemented the tableChanged(TableModelEvent event) methode, But I'm not able to detect the right column that was modified. I got col -1, while i'm editing the second or a specific column.
Here is my implementation of tableChanged(TableModelEvent event) methode:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
public class JTableTest extends JDialog{
private JTable myTable ;
public JTableTest() {
super();
myTable = new JTable(new myTableModel());
getContentPane().add(new JScrollPane(myTable),BorderLayout.CENTER);
pack();
}
class myTableModel extends AbstractTableModel implements TableModelListener{
Object[][] donnees = {
{"Johnathan", "Sykes","Natation"},
{"Nicolas", "Van de Kampf","FOOTBALL" },
{"Damien", "Cuthbert","TENNIS" },
{"Corinne", "Valance","NATATION"},
{"Emilie", "Schrödinger","FOOTBALL"},
{"Delphine", "Duke","TENNIS"},
{"Eric", "Trump","TENNIS"},
};
String[] entetes = {"Prénom", "Nom","SPORT"};
public myTableModel() {
addTableModelListener(this);
}
public int getRowCount() {
return donnees.length;
}
public int getColumnCount() {
return entetes.length;
}
public String getColumnName(int columnIndex) {
return entetes[columnIndex];
}
public Object getValueAt(int rowIndex, int columnIndex) {
return donnees[rowIndex][columnIndex];
}
@Override
public void tableChanged(TableModelEvent me) {
// I wanna get the right column not -1
System.out.println(me.getColumn());
switch(me.getType()){
case TableModelEvent.UPDATE:
System.out.println("Updating...");break;
case TableModelEvent.INSERT:
System.out.println("Inserting...");break;
case TableModelEvent.DELETE:
System.out.println("Delete...");break;
default :break ;
}
}
@Override
public void setValueAt(Object value, int row, int col) {
donnees[row][col] = (String) value;
fireTableDataChanged();
}
@Override
public boolean isCellEditable(int row, int col) {
return true;
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new JTableTest().setVisible(true);
}
});
}
}
Upvotes: 1
Views: 538
Reputation: 324207
Based on your original question how are we supposed to guess that you are using a custom TableModel? That is why you should always include a proper SSCCE with the question so we don't have to guess what you may or may not be doing.
Why are you using a custom TableModel? There is no need to create a custom model. The DefaultTableModel will do what your custom model does.
Anyway, the problem is indeed with your custom TableModel:
//fireTableDataChanged();
fireTableCellUpdated(row, col);
You didn't change all the data. You only changed the data of one cell so fire the proper event.
Upvotes: 2
Reputation: 392
The method BigDecimal.add(BigDecimal) returns a BigDecimal. It does not reassign the value.
You should use the following code to reassign somme
value:
somme = somme.add((BigDecimal) getValueAt(i, 3));
Upvotes: 0