Kieran France Kieran France is a programmer for IDRSolutions in charge of there internal test suite. In his spare time he enjoys tinkering with gadgets and code.

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.

Do you need to solve any of these problems in Java?

Convert PDF to HTML5
Convert PDF to HTML5
Convert PDF to SVG
Convert PDF to SVG
Convert PDF Forms to HTML5 Forms
View Forms in the browser
Java PDF Reader and Viewer
View PDF Documents
Convert PDF to image
Convert PDF to image
Extract Text from PDF
Extract Text from PDF
Read/Write images in Java
Read/Write images
Replace ImageIO
Replace ImageIO
Convert Image to PDF
Convert Image to PDF
Kieran France Kieran France is a programmer for IDRSolutions in charge of there internal test suite. In his spare time he enjoys tinkering with gadgets and code.

Leave a Reply

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

IDRsolutions Ltd 2021. All rights reserved.