ScrollBar movement not smooth in JScrollpane in Swing

后端 未结 3 1627
予麋鹿
予麋鹿 2021-01-20 15:39

I have a situation like this. I have scrollpane whose viewportView is a JPanel And that JPanel has the layout as BoxLayout. In this panel I add one class which extends JPane

相关标签:
3条回答
  • 2021-01-20 16:23

    Have look at JScrollBar.setUnitIncrement, beacuse bunch of JPanels in the JScollPane has un_natural scrolling in compare with JList, JTable or JTextArea

    example

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class JScrollBarUnitIncrement {
    
        public static void main(String[] args) {
            final JFrame f = new JFrame("");
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(2000, 1));
            for (int i = 0; i != 2000; i++) {
                JButton btn = new JButton("Button 2");
                panel.add(btn);
            }
            final JScrollPane sPane = new JScrollPane(panel);
            final int increment = 50;
            sPane.getVerticalScrollBar().setUnitIncrement(increment);
            KeyStroke kUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
            KeyStroke kDown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
            sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kUp, "actionWhenKeyUp");
            sPane.getActionMap().put("actionWhenKeyUp", new AbstractAction("keyUpAction") {
    
                private static final long serialVersionUID = 1L;
    
                public void actionPerformed(ActionEvent e) {
                    final JScrollBar bar = sPane.getVerticalScrollBar();
                    int currentValue = bar.getValue();
                    bar.setValue(currentValue - increment);
                }
            });
            sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kDown, "actionWhenKeyDown");
            sPane.getActionMap().put("actionWhenKeyDown", new AbstractAction("keyDownAction") {
    
                private static final long serialVersionUID = 1L;
    
                public void actionPerformed(ActionEvent e) {
                    final JScrollBar bar = sPane.getVerticalScrollBar();
                    int currentValue = bar.getValue();
                    bar.setValue(currentValue + increment);
                }
            });
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(sPane);
            f.pack();
            SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    f.setVisible(true);
                }
            });
        }
    
        private JScrollBarUnitIncrement() {
        }
    }
    
    0 讨论(0)
  • 2021-01-20 16:34

    As @mKorbel notes, both JTable and JList implement Scrollable for convenient scroll increments, and they both use the flyweight pattern for rendering speed. If you can't use either component directly, you can still use the patterns. The tutorial includes Scrollable examples, and there's a CellRendererPane example here.

    0 讨论(0)
  • 2021-01-20 16:35

    It is never good to populate such huge no. of rows in JScrollPane. Because, the visible portion is only around let's say 20 to 30 rows in viewport depending on the height of the scrollpane and the height of your RowPanel. So, why to populate such huge rows at once ? The problem with the smoothness is because there might be exception (see the console ). So, resolve this, I see two options for you. One is to use pagination and another is to allow users to enter some search criteria to filter out the unwanted records.

    0 讨论(0)
提交回复
热议问题