JTable and DefaultTableModel
-
19-06-2021 - |
Question
I have a table that gets information from my local MySQL server. It reads the data very well and post it on GUI.
My question is , how can I refresh my table when I change my table command, for example:
private String sql = "select * from profildb.tbl_detailed"; //to
private String sql = "select * from profildb.tbl_detailed where Y.."; //this
This action will be handled in my Button Action Listener ;
JButton btnOK = new JButton("");
btnOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if( (tfBirinci.getText().isEmpty() || tfBirinci.getText() == null) && (tfIkinci.getText().isEmpty() || tfIkinci.getText() == null ))
{
taLog.setText("Database alani bos birakilamaz...\n");
}
else if ( (!(tfBirinci.getText().isEmpty() )) && (tfIkinci.getText().isEmpty() || tfIkinci.getText() == null ) )
{
sql = ("SELECT * FROM " + tfBirinci.getText());
taLog.setText("Komut elde edildi : " + sql + "\n");
System.out.println("aaaa " + tfBirinci.getText());
//anaFrame.dispose();
//databaseHistoryCalistir(); doesnt work
}
else if ( ( !(tfBirinci.getText().isEmpty() ) && !(tfBirinci.getText() == null) ) && ( !(tfIkinci.getText().isEmpty() ) && !(tfBirinci.getText() == null) ) )
{
sql = ("SELECT * FROM " + tfBirinci.getText() + " WHERE " + tfIkinci.getText());
taLog.setText("Komut elde edildi : " + sql + "\n" );
System.out.println("bbbb " + tfBirinci.getText());
//anaFrame.dispose();
//databaseHistoryCalistir(); doesnt work
}else
taLog.setText("Lütfen Database alanini doldurunuz, aksi taktirde komut elde edilemez...\n");
}
});
So , what I need to implement for ONLY update the table when I change the statement of my string ?
Thanks in advance. (It would be awesome to give an example about DefaultTableModel)
Edit , You can see my full code here : http://pastebin.com/eQCJVuKn
Solution
1) use Table from Database by @camickr
2) use one of ResultsetTableModel
3) call SQL statement from Runnable#Thread
, but output to the XxxTableModel
must be inside invokeLater
, more in the Concurency in Swing about Event Dispatch Thread (EDT)
4) call SQL statement from SwingWorker, then output from progress()
, publish()
or done()
should be on EDT
5) DefaultTableModel
doesn't required override methods fireXxxTableXxx
, all of them are correctly implemented
6) difference betweens logics Table from Database (ResultsetTableModel)
and Runnable#Thread(SwingWorker)
is
Table from Database (ResultsetTableModel)
calling all updates on EDT, then GUI waiting for all event are done, during loading data from database is GUI unresponsible or freezeRunnable#Thread(SwingWorker)
all updates are from backgourng task and GUI is resposible for Mouse and Keyboard events,