This is my Observer class:
public class GUIController implements Observer {
private AutoRefresh ar;
private Java_SystemTray jst;
public GUIController(){
ar=new AutoRefresh();
jst=new Java_SystemTray();
jst.addObserver(this);
ar.addObserver(this);
actionAdderButtons(gui.getButtons());
TLRefresher();
}
@Override
public void update(Observable arg0, Object arg1) {
if(arg0==jst){
gui.onSysTrayClick();
}
if(arg0==ar){
try {
gui.refreshTL();
} catch (TwitterException e) {
e.printStackTrace();
}
}
}
}
and these are my Observable classes:
public class AutoRefresh extends Observable implements Runnable {
@Override
public void run() {
while(!Thread.currentThread().isInterrupted())
{
try {
Thread.sleep(90000);
setChanged();
notifyObservers();
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
public class Java_SystemTray extends Observable{
public void systray_show(){
String iconPath = "res/Twitter_logo_blue.png";
SystemTray systemTray = SystemTray.getSystemTray();
TrayIcon trayIcon = new TrayIcon(new ImageIcon(iconPath, "omt").getImage(), "Twitter Poster");
trayIcon.setImageAutoSize(true);
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
systemTray.remove(trayIcon);
setChanged();
notifyObservers();
}
};
trayIcon.addMouseListener(mouseAdapter);
try {
systemTray.add(trayIcon);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I tried to cut out as much as possible, and I hope it still transfers the problem. Not trying to bother you with my code but I feel like I have to post a little more than one or two lines as I can't seem to figure out the problem at all.
However, my problem here is, that my code passes through the passage where I setChanged(); and notifyObservers(); , but my Observers update method is not being called at all. It's my first time working with observers and observables, but I don't really see what I'm doing wrong here.
I'm also not quite sure how to check where exactly the problem lies. Either the Observables don't notify the Observer class properly, or the Observers update method is not being called at all. It might be both, I don't really know.
As I stated, I did some checking whether the setChanged(); and notifyObservers(); are being passed, and they do, indeed, without shooting off any errors. I'm really lost right now, as I feel like I did everything according to the documentations and tutorials I read through.
If someone could help me out, I'd be very thankful!
EDIT: I could gather some more information. Obviously, the setChanged(); works, which means the Observable is getting flagged as "Changed".
The main problem here is that the AutoRefresh Runnable is not being run, so you need an
ar.run()
statement in there. With that, it will work.
In comments - OP indicates that some of the thread code was being executed without ar.run()
being called in another version. Without seeing the code, I can't be sure but my guess would be that some of the functions were called in the object constructor. It would even be possible to run the object on construction, if this.run() was called in the object constructor. Obviously this is something of a guess without seeing the exact code that exhibited the reported behaviour.