aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Bad Background Refresh Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Bad Background Refresh" Watch "Bad Background Refresh" New topic
Author

Bad Background Refresh

Randall Fairman
Greenhorn

Joined: Apr 18, 2011
Posts: 29

This is complicated to explain, but the gist is that the JVM is painting a JPanel to its background color when I don't want that to happen. Or, if I fix that, then one window's outline shows through and appears in the content area of a window that's on top of it.

The UI consists of a JFrame holding a JDesktopPane which holds three JInternalFrame objects. One of these JInternalFrame objects contains an instance of my own class, called STextArea. In some round-about way, the problem seems to be due the fact that STextArea starts a Timer, where each time that the Timer goes off, the action command sets a flag and calls repaint() for STextArea. This flag indicates that the next call to paintComponent() should only draw a caret (in xor mode) and draw nothing else. In the example code shown below, I just have paintComponent() do nothing and return when that flag is set rather than drawing the caret since the problem still manifests.

Here's the code -- I stripped it down as much as possible. If this is executed, you will see three sub-windows, where the one in the rear is titled Problem. Click on the Problem window to bring it to the front. The click on the Messages window at the lower-right to bring it to the front. You'll see that the refresh of the middle Messages window bleeds through to the content area of the Problem window. It bleeds through because of the calls to isOpaque(). However, if I get rid of those settings, then the call to STextArea.paintComponent() causes the JVM to fill the contents with the background color the first time that the Timer goes off.

Randall Fairman
Greenhorn

Joined: Apr 18, 2011
Posts: 29

To help give a more complete picture of what should happen, here's a code snippet that could be used in place of the STextArea.paintComponent() given above. It will flash a small yellow circle over the text, much like the way a caret blinks. The more fundamental problem(s) remain, but this shows the role of the Timer.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
You don't need all the synchronized methods. Swing painting code and Timer code is executed on the EDT which is single threaded.

I don't see the problems using JDK6_7 on XP.
Randall Fairman
Greenhorn

Joined: Apr 18, 2011
Posts: 29

You don't see the problem? That's weird. I'm using XP and Java 1.6.0_26.

No matter what combination of setOpaque() values I use, I get some kind of strange behavior. Either the STextArea gets erased when the Timer trips or the window experiences bleed-through from a window that's behind it.
Randall Fairman
Greenhorn

Joined: Apr 18, 2011
Posts: 29

To make sure that there wasn't some problem with my IDE or some other oversight that I might have made due to my environment, I copied the code directly from the posting above to several .java files, then compiled and ran it from the command-line with javac and java. The problem is definitely there. Even if it's specific to my version of Java (which I have not tested), the fact that this problem arises at all is troubling. Either there's an error on my part (but I don't see it) or the idea of updating only a portion of a window, while leaving the rest unchanged, in response to a Swing Timer is off-limits as a strategy.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bad Background Refresh