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

  • SEARCH
  • Home
  • 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 8700823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:15:25+00:00 2026-06-13T02:15:25+00:00

I have a JScrollPane containing a panel with a BoxLayout (PAGE AXIS). My problem

  • 0

I have a JScrollPane containing a panel with a BoxLayout (PAGE AXIS).

My problem is that the JScrollPane does not react to mouse wheel events. To make it scroll using the mouse wheel i need to be on the JScrollBar.

I found this thread and i have no MouseMotionListener or MouseWheelListener, only a MouseListener. I think my problem come from the fact that my JScrollPane act on a JPanel that contains other panels itself. So when the mouse is on a panel within the JScrollPane it seems that the event is consumed by this panel i never seen by the scroll pane.

Is there a correct way to make the events caught by the children of the scroll pane visible to this scroll pane?

SSCCE:

enter image description here

Here a simple test case trying to show when i try to do in my Swing application.

The frame:

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
        for (int i = 0; i < 50; i++) {
            jPanel1.add(new TestPanel());
        }
    }

private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1,    javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPane1.setViewportView(jPanel1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
           @Override
            public void run() {
                new NewJFrame().setVisible(true);
           }
        });
    }
}

And the TestPanel definition:

public class TestPanel extends javax.swing.JPanel {

    public TestPanel() {
        initComponents();
    }

    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        jLabel1.setText("jLabel1");

        setBackground(new java.awt.Color(255, 51, 51));
        setLayout(new java.awt.BorderLayout());

        jLabel2.setText("TEST LABEL");
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        add(jLabel2, java.awt.BorderLayout.PAGE_START);

        jTextArea1.setEditable(false);
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setFocusable(false);
        jScrollPane1.setViewportView(jTextArea1);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
   }
}

The JTextArea seems to consume the event since when the mouse cursor is inside it, the scrolling using wheel does not work. I have to put the mouse cursor outside the text area to make it works again.

  • 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-06-13T02:15:27+00:00Added an answer on June 13, 2026 at 2:15 am

    Walter beat me to analysing the issue 🙂

    Adding a bit of detail:

    It’s correct that a JScrollPane supports mouseWheelHandling. According to the rules of mouseEvent dispatching, the top-most (in z-order) component gets the event, and that’s the scrollPane around the textArea. So if wheeling the textarea is not required, a simple solution might be to disable the wheel-support in its scrollPane. And JScrollPane even has api for doing it:

    scrollPane.setWheelScrollingEnabled(false); 
    

    Unfortunately, that doesn’t work. Reason it’s not working is that this property has no effect in the event dispatch chain which ultimately calls into eventTypeEnabled:

    case MouseEvent.MOUSE_WHEEL:
          if ((eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0 ||
              mouseWheelListener != null) {
              return true;
          }
    

    This returns true if a mouseWheelListener is installed – which is done unconditionally by BasicScrollPaneUI, and not removed when the wheelEnabled property is changed (the ui doesn’t even listen to that property …) Plus the listener simply does nothing if the property is false. At least one of those facts is a bug, the ui should

    • either remove/add the listener depending on wheelEnabled
    • or: implement the listener such that it dispatches the event up the chain (as Walter does in his example)

    The first option can be handled by application code:

    scrollPane = new JScrollPane();
    scrollPane.removeMouseWheelListener(scrollPane.getMouseWheelListeners()[0]);
    

    it’s a bit of a hack (as bug-workarounds always are :-), production code would have to listen to the wheelEnable to re-install if needed plus listen to LAF changes to update/re-remove the listeners installed by the ui.

    Implementing the second option in slight modification (as to Walter’s dispatching) by subclassing the JScrollPane and dispatch the event to parent if the wheelEnabled is false:

    scrollPane = new JScrollPane() {
    
        @Override
        protected void processMouseWheelEvent(MouseWheelEvent e) {
            if (!isWheelScrollingEnabled()) {
                if (getParent() != null) 
                    getParent().dispatchEvent(
                            SwingUtilities.convertMouseEvent(this, e, getParent()));
                return;
            }
            super.processMouseWheelEvent(e);
        }
    
    };
    scrollPane.setWheelScrollingEnabled(false); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been unsuccessful getting the mouse wheel to scroll a horizontal jscrollpane. Does
I have a JScrollpane that has a JPanel on the inside (and the panel
I have on my page 2 DIVs that uses jScrollPane and I want each
I have a problem with JTable / JScrollPane . My data table is not
I have JScrollPane that contains a panel like JScrollPane scrollWindow = new JScrollPane(window); I
I have embedded a JTextArea on a JScrollPane and am using that JTextArea for
I have a simple JEditorPane inside a JScrollPane that displays line numbers on the
I have a JScrollPane that holds a JLabel using the following code: //Create TEXT
I have a JScrollpane which contains a scrollable client that changes its size dynamically
I have a JScrollPane with FlowLayout that I want to have a fixed width.

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.