JScrollPane - visual glitch while scrolling

JScrollPane had several topics about similar glitches, scrolling through them like:

But they are missing SSCCE and some explanation for this case, so I will add that it is missing.

First of all, here is a small example written in pure Swing without using third-party code:

public class ScrollGlitchExample extends JFrame { public ScrollGlitchExample () throws HeadlessException { super (); final JPanel top = new JPanel (); top.setPreferredSize ( new Dimension ( 300, 50 ) ); top.setBorder ( BorderFactory.createLineBorder ( Color.BLACK ) ); add ( top, BorderLayout.NORTH ); final JPanel panel = new JPanel ( new GridLayout ( 500, 1 ) ); for ( int i = 0; i < 500; i++ ) { panel.add ( new JButton ( "button" + i ) ); } final JScrollPane scroll = new JScrollPane ( panel ); scroll.setPreferredSize ( new Dimension ( 300, 300 ) ); add ( scroll, BorderLayout.CENTER ); final JPanel bottom = new JPanel (); bottom.setPreferredSize ( new Dimension ( 300, 50 ) ); bottom.setBorder ( BorderFactory.createLineBorder ( Color.BLACK ) ); add ( bottom, BorderLayout.SOUTH ); setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE ); pack (); setLocationRelativeTo ( null ); } public static void main ( final String[] args ) { SwingUtilities.invokeLater ( new Runnable () { @Override public void run () { new ScrollGlitchExample ().setVisible ( true ); } } ); } } 

This is a small example - two panels at the top and bottom and scrolling with a lot of content in the middle of the frame. So, here is what the glitch looks like in this particular example:

Scroll glitch screenshot

To play it: You will have to scroll down the JScrollPane using the mouse wheel only , scrolling by dragging the scrollbar does not cause this problem, possibly due to the massive number of reviews or some other minor differences. When scrolling, you will see white lines overlapping buttons. In BLIT_SCROLL_MODE, JScrollPane simply copies the details written earlier in order to maximize scroll speed and minimize RAM used, but it seems to have some kind of error.

To make the effect even more terrifying, you can increase the scroll speed:

 scroll.getVerticalScrollBar ().setUnitIncrement ( 30 ); 

And you will see something like this after scrolling for some time:

enter image description here

This crash first appeared when I switched from Windows 7 to Windows 8, so it also looks like a problem with Windows 8. It can be played using any version of the JDK (6/7/8) on Windows 8. I'm not sure whether it can be reproduced in other systems.

One more note - it seems that the problem is similar in Windows 8 applications that are not even related to Java. For example, I often saw this problem in the Skype chat window, sometimes in text editors and other applications that often use scrolling. At the same time, all these applications (the same versions) do not have this problem in other versions of Windows.

So this is probably some common problem with Windows 8, but I can't be 100% sure, because there is a simple (but not very good) way to work around the code:

 scroll.getViewport ().setScrollMode ( JViewport.BACKINGSTORE_SCROLL_MODE ); scroll.getViewport ().setScrollMode ( JViewport.SIMPLE_SCROLL_MODE ); 

Using one of these scroll modes instead of JViewport.BLIT_SCROLL_MODE (which is configured as the default scroll mode in Swing because it is most efficient) fixes the problem. Using JViewport.BACKINGSTORE_SCROLL_MODE instead of the default mode may be the best workaround, but still it is a workaround, as well as some of the minuses described in the commentary to this scroll mode:

 /** * Draws viewport contents into an offscreen image. * This was previously the default mode for <code>JTable</code>. * This mode may offer advantages over "blit mode" * in some cases, but it requires a large chunk of extra RAM. * * @see #setScrollMode * @since 1.3 */ public static final int BACKINGSTORE_SCROLL_MODE = 2; 

To summarize:

  • This glitch appears in almost any scroll in Windows 8
  • It can be played by scrolling with the mouse wheel, but not dragging the scroll bar
  • You can fix it by changing the scroll mode in JViewport to JScrollPane

And my questions are:

  • Can anyone confirm that this problem appears on any OS other than Windows 8?
  • What could be the real source of this problem?
  • Is there any known / good way to fix this without changing the scroll mode?

Update 1

Since this problem seems to be specific to my hardware / software, I will try to update various materials (system, video drivers, do some cleaning). But this can be useful for those who are faced with the same problem, so I will post more updates if I understand what exactly caused this problem and how to fix it.


Update 2

After installing a dozen Windows updates (Windows still has 8, not 8.1), this problem disappeared. I'm not sure which update fixes this problem, but these are certainly some of the major system updates. Therefore, basically this problem can occur if you use an earlier version of Windows 8 without installing the latest updates.

+6
source share
1 answer

While GridLayout(0, 1) can arrange an arbitrary number of components in a column, the result does not scale well for more than a few hundred cells. Profile . Instead, use a component that displays only visible cells, such as a JList or JTable , as suggested here .

+4
source

Source: https://habr.com/ru/post/969891/


All Articles