java – JTable和DefaultTableModel
作者:互联网
我有一个表从我的本地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的例子会很棒)
编辑,你可以在这里看到我的完整代码:http://pastebin.com/eQCJVuKn
解决方法:
1)使用Table from Database by @camickr
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