Re-paint on translucent frame/panel/component.

后端 未结 4 1279
一个人的身影
一个人的身影 2020-11-22 15:42

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....

4条回答
  •  不知归路
    2020-11-22 16:01

    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.

提交回复
热议问题