Reputation: 27
i am a noob at java. This is my first time use timer and timerTask class in java.
Purpose of my app:
It is multi client app that implemented with MySQL. My app is to read my database data repeatly. If database updated, then i want every client's panel updated as well. so I assume i need a timer class that can automatically perform repeating query that read my database and then make some change on the component of the client side.
Problem:
I look thought some tutorial and i found this way to do it. Since this.updateTableStatus() method is in my Table class, how can i use that method in my MyTimer(timerTask) class.
public class Table extends javax.swing.JFrame {
public Table() {
initComponents();
MyTimer myTime = new MyTimer();
}
public void updateTableStatus(){
// this is where I refresh my table status which is reading database data and make some change.
}
class MyTimer extends TimerTask{
public MyTimer() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(this, new java.util.Date(), 1000);
}
public void run(){
this.updateTableStatus(); // this is not working!!!, because they r not in the same class. I need help to solve this problem.
}
}
}
Help me out guys. thank you so much.
Upvotes: 0
Views: 108
Reputation: 48212
this.updateTableStatus();
tries to reference MyTimer's updateTableStatus()
method (but there is no such method). To reference Table's updateTableStatus()
method, you can change it to
Table.this.updateTableStatus();
NOTE:
Seriously, I believe that checking the DB every second from each client to see if anything has changed is a horribly bad app design. I suggest you post a new question, explaining your current architecture and requirements, and asking advice about how to watch for DB changes efficiently.
Upvotes: 0
Reputation: 324108
It is probably better to use a Swing Worker and then publish the results when the data changes. This way you don't block the EDT while doing your database query.
If you are going to use a TimerTask, then you need to wrap any updates to the TableModel in a SwingUtilities.invokeLater().
Upvotes: 1