It has been apparent for a while that there was a short coming in Swing where a grey box appears within display area when scrolling is used. This has been a problem that was particularly difficult to hunt down and prevent. That is until I finally found the cause and the details about what was actually going on. So, for those who have encountered similar issues I’m going to detail the issue here. Below is a screen shot of the actual issue caused only as page backgrounds drawn.
So, wheres the grey box coming from?
After looking through our display methods I managed to issolate a single method where before it the display was correct and after it a grey box is present. This method is JComponent.scrollRectToVisible(Rectangle rect). It appears that this method sets certain areas for redraw in a given way that leaves the area transparent.
You see, the grey box is appearing in our example on a JComponent that has a custom dark grey background. When checking the light grey box I found that the RGB values were identical to the default JComponent background. To begin with I thought this might be that an area of the JComponent was having the default background drawn. After a short investigation I found this to be very unlikely. I came to the idea that the areas may have become transparent by one of those eurika moments while making a cup of tea.
To test this I altered the JComponents parents background to a color that would be incredibly obvious such as bright red. Bingo. When I ran the scenario that would show the grey box we have bright red areas instead of grey.
Now I don’t have an indepth understanding of what is happening under the hood in this case but what I can tell you is that this issue is taking place due to the workings of the Swing method mentioned earlier and you can add a work around by setting the parents background color to the same as the JComponent that is experiencing the issue. Below shows the fix whilst page backgrounds are drawn.
So, long story short. If you find yourself experiencing a strange grey box appearing in your display pane you should just need to set your JComponents parent to have the same background color in order to avoid this effect.
Do you need to work with PDF Files in Java?
|Try JPedal - Our Enhanced Document Viewing, Printing and Extracting Solution|