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.

Are you a Java Developer working with PDF files?

Find out why you should be using JPedal

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.



Our software libraries allow you to

Convert PDF to HTML in Java
Convert PDF Forms to HTML5 in Java
Convert PDF Documents to an image in Java
Work with PDF Documents in Java
Read and Write AVIF, HEIC, WEBP and other image formats
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.