0

I am having problems with scrolling on a JScrollPane.

I have a list of images in a JList which I am placing on a JScrollPane. The list can be very long, sometimes hundreds or thousands of images. I add the JScrollPane to a JPanel, and the image gets drawn successfully, the JScrollPane adds scroll bars when needed and the scroll bars can move the image. The response from the scrollbars are good if the list of images is simple (10 - 20).

However, lists that run into the hundreds cause the vertical scrolling to initially run very slowly until all the images have been shown, then the scrolling speed returns to normal.

Can anyone suggest a technique to speed the initial scrolling response? I have experimented with adjusting the viewport settings (eg JViewport.BACKINGSTORE_SCROLL_MODE, JViewport.BLIT_SCROLL_MODE) but these did not lead to any improvement in the initial scrolling response.

The code that I am using is represented below.

    prList = new JList(prListModel); // a list of interactive images for the JScrollPane

    MouseAdapter listener = new PrChangeListener(prList, this);
    prList.addMouseListener(listener);
    prList.addMouseMotionListener(listener);

    renderer = new PrRenderer();
    renderer.setPreferredSize(getIconPreferredSize());
    prList.setCellRenderer(renderer);
    prList.setBackground(Color.WHITE);
    prList.setSelectionBackground(new Color(225,225,225));


    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

    //try separate linked scroll bar for scale bar ...

    prListModel scaleBarListModel = new prListModel();
    scaleBarListModel.addElement(intArray[0]); // a scale bar to sit above the JScrollPane

    scaleBarList = new JList(scaleBarListModel);
    scaleBarList.setCellRenderer(renderer);
    scaleBarList.setBackground(Color.WHITE);
    scaleBarList.setSelectionBackground(Color.WHITE);
    scaleBarScrollPane = new JScrollPane(scaleBarList,JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    prListModel.removeElementAt(0);

    prScrollPane = new JScrollPane(prList);
    prScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    prScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    scaleBarScrollPane.getHorizontalScrollBar().setModel(prScrollPane.getHorizontalScrollBar().getModel());
    JPanel twoPanePanel= new JPanel(new GridBagLayout());


    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.fill = GridBagConstraints.BOTH;

    gbc.gridx=0;
    gbc.gridy=0;
    gbc.weightx=1;
    gbc.weighty=0.1;

    twoPanePanel.add(scaleBarScrollPane, gbc);

    gbc.gridx=0;
    gbc.gridy=1;
    gbc.weighty=0.9;

    twoPanePanel.add(prScrollPane,gbc);

    add(twoPanePanel);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ross
  • 1
  • 2
  • 1
    There are a few possible issues. It will depend on how the images are been loaded and rendered by the `JList` – MadProgrammer Apr 08 '15 at 06:44
  • The problem could be in the PrRenderer code. E.g. if you load images there. – StanislavL Apr 08 '15 at 06:53
  • Thanks for your prompt response @MadProgrammer. The images are loaded into the JList from a HashMap of image icons. The rendering is done using the following: public PrRenderer() { setOpaque(true); setHorizontalAlignment(CENTER); setVerticalAlignment(CENTER); } – Ross Apr 08 '15 at 06:56
  • Do you render them using a `ListCellRenderer`? – MadProgrammer Apr 08 '15 at 06:57
  • Yes @MadProgrammer, the PrRenderer implements ListCellRenderer. – Ross Apr 08 '15 at 07:00
  • What size are the images? How are they loaded? – MadProgrammer Apr 08 '15 at 07:02
  • 1
    Yep, going to need to see `PrRenderer`. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Apr 08 '15 at 07:07
  • 1
    Use `JTable`, which does flyweight rendering, and load in the background, for [example](http://stackoverflow.com/a/25526869/230513). – trashgod Apr 08 '15 at 10:40

0 Answers0