I\'m trying to create a translucent window with Java on OSX and add a JLabel
to it.
This JLabel
changes its text every second....
The problem may also have to do with the fact that you're setting the JLabel
's text from a thread that is not the Event Dispatch Thread.
There are two ways to solve this. Without testing your problem, I'd solve it by using the javax.swing.Timer
class, instead of the java.util.Timer
class. javax.swing.Timer
will ensure that events are fired on the dispatch thread.
So (untested code):
final ActionListener labelUpdater = new ActionListener() {
private int i;
@Override
public final void actionPerformed(final ActionEvent event) {
label.setText("Hola " + this.i++);
}
};
final javax.swing.Timer timer = new javax.swing.Timer(1000L, labelUpdater);
The other way to solve it is to continue to use java.util.Timer
but to make sure that you use EventQueue.invokeLater(Runnable)
to ensure that updates to the label take place on the EDT.