Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:19:21+00:00 2026-05-23T14:19:21+00:00

A JPanel has a JScrollPane that contains yet another JPanel or two. My life

  • 0

A JPanel has a JScrollPane that contains yet another JPanel or two. My life depends on increasing the scroll speed using a keyboard’s directional arrows. After careful deliberation, the powers that be decided that: sc.getVerticalScrollBar().setUnitIncrement(240); should only be applicable to a mouse, in a clever ruse to elicit minor annoyances amongst java developers. Is there anything that can be done to increase scroll speed? My life hangs in the balance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:19:22+00:00Added an answer on May 23, 2026 at 2:19 pm

    You have to use a combination of InputMap.put and ActionMap.put to capture the keyboard events for the components contained on your JScrollPane and process the keyboard events when the JScrollPane has the focus. Since the default increment value for scrolling is 1 you should add or substract the desired increment value to the current value of the scrollbar for JScrollPane which you can get with JScrollPane.getVerticalScrollBar().getValue() and set with JScrollPane.getVerticalScrollBar().setValue(int).

    An example of capturing events for the contained elements withing JScrollPane can be done with this code, I’ve done with buttons, but you get the point (Sorry for the bad organization of the code):

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Test
    {
        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 = 5000;
            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")
                {
                    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")
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        final JScrollBar bar = sPane.getVerticalScrollBar();
                        int currentValue = bar.getValue();
                        bar.setValue(currentValue + increment);
                    }
                }
            );
            f.add(sPane);
            f.pack();
    
    
    
            SwingUtilities.invokeLater(
                    new Runnable()
                    {
                        public void run()
                        {
                            f.setVisible(true);
                        }
                    }
                );
        }
    }
    

    We register to listen and process that event with:

    sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kUp,"actionWhenKeyUp");
    sPane.getActionMap().put("actionWhenKeyUp",
        new AbstractAction("keyUpAction")
        {
            public void actionPerformed(ActionEvent e)
            {
                final JScrollBar bar = sPane.getVerticalScrollBar();
                int currentValue = bar.getValue();
                bar.setValue(currentValue - increment);
            }
        }
    );
    

    The key code that perform the value of JScrollBar increment is of the AbstractAction (in this case when the user press the up key).

            public void actionPerformed(ActionEvent e)
            {
                final JScrollBar bar = sPane.getVerticalScrollBar();
                int currentValue = bar.getValue();
                bar.setValue(currentValue - increment);
            }
    

    What you should do is to complete the events when your JScrollPane has the focus, but that should be trivial.

    Hope it helps to save your life 😛 or at least serve you as a starting point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JScrollpane that has a JPanel on the inside (and the panel
I have a JPanel that has several (25) JLabel s on it, using a
Is there a way to convert a JPanel (that has not yet been displayed)
My code has a JPanel that contains three collapsible JPanel s. The outer JPanel
Right, I have a JTabbedPane that has a JPanel that contains a JLabel and
Can JPanel s background be set to transparent? My frame is has two JPanel
I have a JPanel that encapsulates two JPanels, one on top of the other.
I'm having a JPanel bean which inside has two JToggle buttons.I compile and add
I have a top level container (JFrame) that contains two JPanels. One of the
I have a JScrollPane , which has a JPanel for its content pane. To

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.