I have a class called GUI which basically creates a latout using Swing. In that class i have a method called "log" which is supposed to add a new line to a textarea in the layout.
The problem is that whenever i call the function from outside of the GUI class, nothing happens. If i call the method from within the class it adds a line to the textarea as it's supposed to do.
I have set the method and all the variables it calls to public static, and i don't get any errors. It just doesn't do anything when i call the method from the outside.
Any ideas?
Edit:
Here's the method within the GUI class:
public static void log(String inputString) {
logConsole.append(inputString + "\r\n");
}
At the bottom of the class swing declared the textarea, and i just modified it to be public static instead of private.
public static javax.swing.JTextArea logConsole;
Can't post more code, hope this is at least a little bit helpful? :/
It's most likely a concurrency issue with Swing. Since Swing is single-threaded, Swing components need to be modified in the Event Dispatch Thread (ie EDT). For more information, see Concurrency in Swing .
EDIT -
If this is indeed a concurrency issue, then one quick workaround would be to use SwingUtilities
. In particular, isEventDispatchThread()
and invokeLater(...)
. For instance,
if(!SwingUtilities.isEventDispatchThread()){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
GUI.log("foo"); // modify textarea in EDT
}
});
}
else{
// your problem lies elsewhere
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.