Kieran France Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He spends his time working on the the JPedal library and our internal test suite..

Grey box appears within display area

1 min read

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.

Pages with grey box issue present.
Swing issue causing grey box on background

 

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.

 

The Fix.

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.

Pages with corrected background after fixing grey box issue
Display after fixing grey box issue

 

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.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the  Blog RSS.

Kieran France Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He spends his time working on the the JPedal library and our internal test suite..

Leave a Reply

Your email address will not be published. Required fields are marked *