我有一個表從我的本地MySQL服務器獲取信息.它很好地讀取數據并將其發布在GUI上.
我的問題是,當我更改table命令時,如何刷新表格,例如:
private String sql = "select * from profildb.tbl_detailed"; //to
private String sql = "select * from profildb.tbl_detailed where Y.."; //this
此操作將在我的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");
}
});
那么,當我更改字符串的語句時,我需要實現什么才能更新表?
提前致謝. (給出一個關于DefaultTableModel的例子會很棒)
解決方法:
2)使用ResultsetTableModel之一
3)從Runnable #Thread調用SQL語句,但輸出到XxxTableModel必須在invokeLater內部,更多在Concurency in Swing中關于事件調度線程(EDT)
4)從SwingWorker調用SQL語句,然后從progress(),publish()或done()輸出應該在EDT上
5)DefaultTableModel不需要覆蓋方法fireXxxTableXxx,所有這些都正確實現
6)來自數據庫(ResultsetTableModel)和Runnable #Thread(SwingWorker)的邏輯表之間的區別是
>數據庫表(ResultsetTableModel)調用EDT上的所有更新,然后GUI等待所有事件完成,從數據庫加載數據期間GUI不負責任或凍結
> Runnable #Thread(SwingWorker)所有更新都來自backgourng任務,GUI可用于鼠標和鍵盤事件,
標簽:java,mysql,swing,jtable,defaulttablemodel
來源: https://codeday.me/bug/20190630/1332781.html