Hy ,
I have a problem with the TableModelListener , my application should print out the cell were the content has been modfied but it does notheing.
Here is my code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
/**
*
* @author Pilu
*/
public class Afisare extends javax.swing.JFrame implements ActionListener, TableModelListener {
//private JButton getValue;
private Vector<Vector<String>> data;
private Vector<String> header;
/** Creates new form Afisare */
public Afisare() throws Exception {
DBEngine dbengine = new DBEngine();
DefaultTableModel tableModel;
data = dbengine.getTabel();
header = new Vector<String>();
header.add("ID");
header.add("Nume");
header.add("Prenume");
header.add("Pret");
tableModel = new DefaultTableModel(data, header);
tableModel.addTableModelListener( this );
initComponents();
}
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
DefaultTableModel model = (DefaultTableModel) this.jTable2.getModel();
int[] rows = jTable2.getSelectedRows();
String connectionURL = "jdbc:mysql://localhost:3306/bazadedate";
Connection connection;
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "123456");
String query = "SELECT * FROM tabel ORDER BY id DESC LIMIT 1" ;
Statement st = connection.createStatement();
ResultSet result = st.executeQuery(query);
String textField1 = jTextField1.getText();
String textField2 = jTextField2.getText();
String textField3 = jTextField3.getText();
while(result.next()){
int lastRowValue = result.getInt("id");
model.addRow(new Object[]{(lastRowValue+1),textField1,textField2,textField3});
st.executeUpdate("INSERT INTO tabel (nume,prenume,pret) VALUES ('"+textField1+"','"+textField2+"','"+textField3+"')");
}
}
catch(Exception e){
System.out.println("The exception is " + e);
}
}
public void tableChanged( TableModelEvent e ) {
DefaultTableModel model = (DefaultTableModel)e.getSource();
int row = e.getFirstRow();
int column = e.getColumn();
String cellValue = String.valueOf( model.getValueAt(row, column) );
System.out.println("Value at (" + row + "," + column + ") changed to " + "'" + cellValue + "\'");
}
public void actionPerformed( ActionEvent evt ) {
DefaultTableModel model = (DefaultTableModel)evt.getSource();
int row = jTable2.getSelectedRow();
int column = jTable2.getSelectedColumn();
if ( evt.getSource() == jButton3 ) {
String value = (String) model.getValueAt(row,column) ;
System.out.println("Value at (" + row + "," + column + ") is " + "\'" + value + "\'");
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
new Afisare().setVisible(true);
}catch(Exception e){e.printStackTrace();}
}
});
}
The problem might be in the Afisare() method , but i couldn`t find it.