このアプリケーションの目的は、テーブルをクエリし、その情報を取得してJTableを更新することです。現在、ThreadTask()はテーブルを照会し、情報を取得できます。私の質問は、データベースから取得した情報でJTable GUIオブジェクトを更新するにはどうすればよいですか?
public class AdminManager extends JFrame {
public static void main(String[] args) throws InterruptedException {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
AdminManager frame = new AdminManager();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
// Setup connection pool
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new ThreadTask(connection), 2000, 100, TimeUnit.MILLISECONDS);
}
/**
* Create the frame.
*/
public AdminManager() {
// Setup GUI
DefaultTableModel model = new DefaultTableModel();
model.addColumn("#");
tableQueue = new JTable(model);
tableQueue.getColumnModel().getColumn(0).setPreferredWidth(3);
scrollPane.setViewportView(tableQueue);
}
class updateTable extends SwingWorker<Void, String> {
@Override
protected Void doInBackground() throws Exception {
model.addRow(new Object[]{order_num});
return null;
}
}
}
class grabData implements Runnable {
private Connection connection;
private DefaultTableModel model;
private String order_num;
public grabData(Connection c, DefaultTableModel m) {
connection = c;
model = m;
}
@Override
public void run() {
System.out.println("Working ... ");
String sql = "SELECT * FROM order_queue;";
Statement st;
try {
st = connection.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()) {
order_num = rs.getString("order_num");
System.out.println(order_num);
updateTable.execute()
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}