简体   繁体   English

摇摆:JTable直到在任何地方单击鼠标都不会更新?

[英]Swing: JTable doesn't update until click the mouse anywhere?

Simply I am writing application code that interacts with database (MySQL). 我只是在编写与数据库(MySQL)交互的应用程序代码。 I made everything OK in Swing to work with database. 我在Swing中使一切正常,可以使用数据库。

Refresh code: 刷新代码:
Finally I wrote some code to refresh the data in the JTable to reflect the actual data from the database. 最后,我编写了一些代码来刷新JTable中的数据,以反映数据库中的实际数据。

This is useful in a scenario like this. 在这种情况下这很有用。 If I added a new record to JTable, generally this record will be added to the underlying database. 如果我向JTable添加了一条新记录,通常该记录将被添加到基础数据库中。 But if I added a record that duplicates an existing one, the underlying database will not insert it, but it will be displayed in the JTable (which has no corresponding record in real database). 但是,如果我添加了一条复制现有记录的记录,则基础数据库将不会插入该记录,而是将其显示在JTable中(在实际数据库中没有相应的记录)。

The code to refresh has the job to get the new data from the database. 刷新代码的工作是从数据库中获取新数据。 It work OK but it doesn't affect (update/refresh) the table until I click the mouse anywhere within the JTable. 它可以正常工作,但是直到我在JTable中的任何地方单击鼠标时,它都不会影响(更新/刷新)表。

How to make a swing component update correctly without poking it. 如何使摆动组件正确地更新而不用戳它。

"But if I added a record that duplicates an existing one, the underlying database will not insert it, but it will be displayed in the JTable... How to make a swing component update correctly without poking it?" “但是,如果我添加了一条复制现有记录的记录,则基础数据库将不会插入该记录,而是将其显示在JTable中……如何正确地摆动组件而不会对其进行戳戳?”

With that said, you could check if the result is 0. If it isn't, then add the row to the table only if the insert is successful. 这样,您可以检查结果是否为0。如果不是,则仅在插入成功的情况下才将行添加到表中。 Something like this 像这样

String text1 = textfield1.getText();
String text2 = textfield2.getText();
String text3 = textfield3.getText();

PreparedStatement ps = conn.prepareStatement("INSERT into table1 values (?, ?, ?)");
ps.setString(1, text1);
ps.setString(2, text2);
ps.setString(3, text3);

int result = ps.executeUpdate();    // check for rows affected

if (result != 0 ) {                 // only if result != 0 do we update the table
    DefaultTableModel model = (DefaultTableModel)jTable.getModel();
    Object[] row = { text1, text2, text3 };
    model.addRow(row);
} else {
    someStatusLabel.setText("That record already exists in the Database");
}

How to make a swing component update correctly without poking it. 如何使摆动组件正确地更新而不用戳它。

  1. update the TableModel directly with the changes 使用更改直接更新TableModel
  2. make sure the updates to the model are done on the Event Dispatch Thread. 确保在事件调度线程上完成了对模型的更新。

Your code to access the database should be done on a non EDT thread so you don't prevent the GUI from responding to events. 您访问数据库的代码应在非EDT线程上完成,因此不会阻止GUI响应事件。 So you should probably be using a SwingWorker for this code. 因此,您可能应该为此代码使用SwingWorker。 Then you publish the results so the model can be updated on the EDT. 然后发布结果,以便可以在EDT上更新模型。

Read the section from the Swing tutorial on Concurrency for more information. 阅读Swing 并发教程中的有关更多信息的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM