Salut,
Pour récupérer la valeur d'une cellule:
-> table.getValueAt(row, column)
Pour modifier la valeur d'une cellule:
-> table.setValueAt(value, row, column)
Exemple:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class JTableUpdate {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rowData[][] = { { "1", "2.5", "" }, { "4", "50", "" } };
Object columnNames[] = { "Nb", "Price", "Total" };
final JTable table = new JTable(rowData, columnNames);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
JButton button = new JButton();
button.setText("Total");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/* pour toutes les lignes */
for(int i = 0; i < table.getRowCount(); i++) {
int nb = Integer.parseInt("" + table.getValueAt(i, 0));
float price = Float.parseFloat("" + table.getValueAt(i, 1));
float total = nb * price;
table.setValueAt(total, i, 2);
}
}
});
frame.add(button, BorderLayout.NORTH);
frame.setSize(300, 150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}