Java invokeAndWait of C# Action Delegate
Posted
by ikurtz
on Stack Overflow
See other posts from Stack Overflow
or by ikurtz
Published on 2010-05-17T16:31:11Z
Indexed on
2010/05/17
20:31 UTC
Read the original article
Hit count: 436
the issue i mentioned in this post is actually happening because of cross threading GUI issues (i hope).
could you help me with Java version of action delegate please?
in C# it is done as this inline:
this.Invoke(new Action(delegate()
{...}));
how is this achived in Java?
thank you.
public class processChatMessage implements Observer {
public void update(Observable o, Object obj) {
System.out.println("class class class" + obj.getClass());
if (obj instanceof String){
String msg = (String)obj;
formatChatHeader(chatHeader.Away, msg);
jlStatusBar.setText("Message Received");
// Show chat form
setVisibility();
}
}
}
processChatMessage is invoked by a separate thread triggered by receiving new data from a remote node.
and i think the error is being produced as it trying to update GUI controls.
do you think this is the reason? i ask because im new to Java and C#, but this is what is going on i think.
SOLUTION:
public class processChatMessage implements Observer {
public void update(Observable o, Object obj) {
if (obj instanceof String){
final String msg = (String)obj;
try {
SwingUtilities.invokeAndWait(new Runnable( ) {
public void run( ) {
formatChatHeader(chatHeader.Away, msg);
jlStatusBar.setText("Message Received");
setVisibility();
}
});
} catch (InterruptedException e){
} catch (InvocationTargetException e){
}
}
}
}
© Stack Overflow or respective owner