12

I am experiencing a problem with Swing that only occurs when the computer monitor is powered off, but my Swing application continues to run in the background. It seems that whenever the monitor is off, Swing/AWT cancels all painting operations, leading to a number of display issues in the GUI that are visible as soon as the monitor turns back on.

For example, when I turn off the monitor using a custom JNI function and subsequently open a simple message dialog, the message dialog is blank when the monitor turns back on:

Blank message dialog

But it paints correctly after the next repaint:

Correctly painted message dialog

Is this the expected behavior of Swing? Is there a way to instruct Swing to continue drawing to the screen even if the monitor is powered off?

EDIT: Here is an SSCCE:

package test;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class App {
    public static void main(String[] args) throws Throwable {
        System.out.println("***** Please turn off the monitor in the next 70 seconds *****");
        Thread.sleep(1000L * 70);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(null, "Test");
            }
        });
    }
}

I am using 64-bit Windows 7 Home Premium SP1 and 64-bit Java 1.6.0_24.

EDIT 2: Here is another program with which I experience the effect of "canceled painting operations":

package test;

import static com.mycompany.Util.turnOffMonitors;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class DialogTest extends JDialog {
    private final JLabel label;

    public DialogTest() {
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        label = new JLabel("Test", JLabel.CENTER);
        label.setOpaque(true);

        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(BorderLayout.CENTER, label);

        this.setPreferredSize(new Dimension(200, 110));
        pack();
        setLocationRelativeTo(null);
        setVisible(true);

        Thread t = new Thread() {
            @Override
            public void run() {
                turnOffMonitors();
                try {
                    Thread.sleep(3000L);
                } catch (InterruptedException ex) { }

                SwingUtilities.invokeLater(new Runnable() {
                   @Override
                   public void run() {
                       label.setBackground(Color.YELLOW);
                   }
                });
            }
        };
        t.start();
    }

    public static void main(String[] args) throws Throwable {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DialogTest();
            }
        });
    }
}

Before the monitor shuts off, I see:

Screenshot of the dialog of DialogTest before the monitor shuts off

With the monitor off, the label background color is changed to yellow in the background. I then move the mouse to turn the monitor back on. The dialog is visually unchanged. It is only after I force a repaint (by ALT-TABbing, for example) do I see the yellow:

Screenshot of the dialog of DialogTest where the main label has a yellow background

EDIT 3: Reported to Oracle as Bug ID 7049597.

Joseph Trebbien
  • 309
  • 1
  • 3
  • 8
  • 1
    I am unable to reproduce this effect. Please supply an [sscce](http://sscce.org) that shows the problem. What platform? – trashgod May 28 '11 at 19:45
  • @trashgod: Supplying an SSCCE will be somewhat difficult because my custom JNI routine to turn off the monitors is proprietary. I'll see what I can come up with, though. I am using 64-bit Windows 7 Home Premium SP1 and 64-bit Java 1.6.0_24. – Joseph Trebbien May 28 '11 at 20:10
  • Write the smallest example that reproduces the effect when starting sleep mode manually. See also [Initial Threads](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod May 28 '11 at 20:29
  • @trashgod: I added an SSCCE. To use it, I temporarily changed my power plan settings to turn the monitor off after 1 minute of inactivity. I then started the program and stopped moving the mouse/typing. After one minute, the screen turned off. I waited another 20 seconds to move the mouse. The monitor turned back on and I saw a blank message dialog. I ALT-TABbed a program on top, minimized it, and saw that the dialog was then correctly painted. – Joseph Trebbien May 28 '11 at 20:59

2 Answers2

2

I then started the program and stopped moving the mouse/typing. After one minute, the screen turned off. I waited another 20 seconds to move the mouse. The monitor turned back on and I saw a blank message dialog.

Using your example, I don't see this on my (non-Windows) platform. You might try the example below, which should alternate between WINDOW_ACTIVATED on wake and WINDOW_DEACTIVATED on sleep. If so, you could extend JDialog and repaint() in windowActivated().

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

/** @see http://stackoverflow.com/questions/6163606 */
public class DialogEventTest extends JDialog {

    public DialogEventTest() {
        this.setLayout(new GridLayout(0, 1));
        this.add(new JLabel("Dialog event test.", JLabel.CENTER));
        this.add(new JButton(new AbstractAction("Close") {

            @Override
            public void actionPerformed(ActionEvent e) {
                DialogEventTest.this.setVisible(false);
                DialogEventTest.this.dispatchEvent(new WindowEvent(
                    DialogEventTest.this, WindowEvent.WINDOW_CLOSING));
            }
        }));
    }

    private static class WindowHandler extends WindowAdapter {

        @Override
        public void windowActivated(WindowEvent e) {
            System.out.println(e);
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
            System.out.println(e);
        }
    }

    private void display() {
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.addWindowListener(new WindowHandler());
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DialogEventTest().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • @trashgod I think that this question is about Concurency not about Power Schemas in Native OS (for todays OS) +1 – mKorbel May 29 '11 at 10:12
  • Although the "Dialog event test" dialog does not receive a window-deactivated event when the monitor powers off, it is interesting that I do not see the effect of "canceled painting operations" in the "Dialog event test" dialog if it opens after the monitor is off. If, however, I replace `new DialogEventTest().display();` with `JOptionPane.showMessageDialog(null, "Test");`, then I see the problem. – Joseph Trebbien May 29 '11 at 15:05
  • @mKorbel: Thanks! I wondered about concurrency too, but the example appear to be correct. @Joseph Trebbien: I vaguely recall a Windows specific problem of this sort that only occurs when the JOptionPane appears while asleep. Is extending `JDialog` a possible workaround? I'm guessing that there's no [top-level container](http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html) to receive `WINDOW_ACTIVATED`. – trashgod May 29 '11 at 15:38
  • @Joseph Trebbien hmmm its looks like as you http://java.sun.com/products/jfc/tsc/articles/mixing/ or http://java.sun.com/developer/technicalArticles/GUI/mixing_components/ – mKorbel May 29 '11 at 15:45
  • @mKorbel: I do not think so. My app is written entirely in Swing, but also the SSCCE that I posted always has this problem. – Joseph Trebbien May 29 '11 at 15:58
  • @Joseph Trebbien I can't problem with that, nor with example by trashgod, EmptyBox is for me always issues with Concurency in Swing, maybe I'm wrong and here is HW&SW problems in Win7, please change http://stackoverflow.com/questions/6051755/java-wait-cursor-display-problem/6060678#6060678 code in Thread.sleep(500) to Thread.sleep(50); if you'll black or empty box then HW&SW problems in Win7 – mKorbel May 29 '11 at 16:09
  • @Joseph Trebbien if you want to see JOptionPane then allows disabled code change if (count == (myTable.getRowCount() * myTable.getColumnCount())) { to if (count == 25 * 6)) { and runProcess = false; must be last of line inside If-block – mKorbel May 29 '11 at 16:17
  • @trashgod: Unfortunately, extending `JDialog` does not seem to be a work-around. I posted another example to my question where the frame extends `JDialog`. With this program, I still experience the "canceled painting operations" effect. It doesn't make a difference if I extend `JFrame` or `JDialog`. – Joseph Trebbien May 29 '11 at 16:38
  • @Joseph Trebbien: As your `DialogTest` also works correctly on my platform, I suspect a platform-dependent problem. In your `DialogTest`, add a Window listener, as shown in my `DialogEventTest`, and call `repaint()` in `windowActivated()`. – trashgod May 29 '11 at 16:53
0

The problem probably has more to do with how it repaints when the screen comes on rather than what happens while it's off. You could check by running a screen recorder.

karmakaze
  • 34,689
  • 1
  • 30
  • 32