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 8522453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:07:21+00:00 2026-06-11T07:07:21+00:00

I’m using a scrollpane set to use a custom scrollbar and among other things

  • 0

I’m using a scrollpane set to use a custom scrollbar and among other things I wanted to install a listener on the thumb of the scrollbar so that whenever the mouse enters the thumb area it could change color or border. I searched BasicScrollBarUI (which is extended by my custom scrollbar UI) and found the method installListeners(), so I overrode it and made it call one more listener for the thumb area.

SSCCE:

public class TestScrollBar extends JFrame {

    public TestScrollBar() {

        JPanel innerPanel = new JPanel();
        innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));
        for (int i=1; i<=10; i++) {
            innerPanel.add(new JLabel("Label "+i));
            innerPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        }

        JScrollPane scrollPane = new JScrollPane(innerPanel);
        scrollPane.setPreferredSize(new Dimension(innerPanel.getPreferredSize().width, innerPanel.getPreferredSize().height/2));

        scrollPane.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        this.setLayout(new BorderLayout());
        this.add(Box.createRigidArea(new Dimension(0, 20)), BorderLayout.NORTH);
        this.add(Box.createRigidArea(new Dimension(0, 20)), BorderLayout.SOUTH);
        this.add(Box.createRigidArea(new Dimension(20, 0)), BorderLayout.EAST);
        this.add(Box.createRigidArea(new Dimension(20, 0)), BorderLayout.WEST);
        this.add(scrollPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TestScrollBar frame = new TestScrollBar();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

And a simple custom UI for the scrollbar:

public class CustomScrollBarUI extends BasicScrollBarUI {

    @Override
    protected void installListeners() {
        super.installListeners();
        scrollbar.addMouseListener(new CustomListener());
    }

    protected class CustomListener extends MouseAdapter {
        public void mouseEntered(MouseEvent e) {
            if (getThumbBounds().contains(e.getX(), e.getY())) {
                System.out.println("THUMB");
            }
        }
    }
}

The listener works fine when the mouse enters the thumb area from the right or left but it doesn’t always work for the top or bottom side. For these sides it only works when the thumb is touching one of the buttons. For example when the scrollbar is at the very top, the listener works only (except for the right-left side) when the mouse enters the thumb area from the top button but not from below. The opposite thing happens when the scrollbar touches the decrease button (only works for right/left/bottom sides but not for top). When the thumb is somewhere between without touching any of the buttons then only the left-right sides work. You can see all this if you run the code.

I also tried to use one of the existing listeners (instead of creating my own), BasicScrollBarUI.TrackListener, where I added a method to listen only for the thumb area, but the results where exactly the same:

public class CustomScrollBarUI extends BasicScrollBarUI {
    @Override
    protected TrackListener createTrackListener(){
        return new TrackListener();
    }

    protected class TrackListener extends BasicScrollBarUI.TrackListener {
        public void mouseEntered(MouseEvent e) {
            currentMouseX = e.getX();
            currentMouseY = e.getY();
            if (getThumbBounds().contains(currentMouseX, currentMouseY)) {
                System.out.println("THUMB");
            }
        }
    }
}
  • 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-11T07:07:23+00:00Added an answer on June 11, 2026 at 7:07 am

    The problem is that you listen for the scrollbar, which means that it is the Thumb and the area where the Thumb can move (the track). Therefore your test condition in the mouseEntered method is not always true. For example, let’s say the thumb is at the top of a vertical scrollbar, entering through the bottom means that you will first enter the scrollbar track and then move the mouse to the thumb area –> no mouse entered event.

    Here is a slightly modified version of your code. What it mainly does is also listen for mouse “move” event (by adding your listener as a MouseMotionListener) and it works as expected:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    import javax.swing.plaf.basic.BasicScrollBarUI;
    
    public class TestScrollBar extends JFrame {
    
        public static class CustomScrollBarUI extends BasicScrollBarUI {
    
            @Override
            protected void installListeners() {
                super.installListeners();
                CustomListener listener = new CustomListener();
                scrollbar.addMouseListener(listener);
                scrollbar.addMouseMotionListener(listener);
            }
    
            protected class CustomListener extends MouseAdapter {
                boolean isInsideThumb = false;
    
                @Override
                public void mouseEntered(MouseEvent e) {
                    handleMouseEvent(e);
                }
    
                @Override
                public void mouseMoved(MouseEvent e) {
                    handleMouseEvent(e);
                }
    
                @Override
                public void mouseExited(MouseEvent e) {
                    handleMouseEvent(e);
                }
    
                private void handleMouseEvent(MouseEvent e) {
                    if (getThumbBounds().contains(e.getX(), e.getY())) {
                        if (!isInsideThumb) {
                            System.out.println("THUMB");
                            isInsideThumb = true;
                        }
                    } else {
                        if (isInsideThumb) {
                            System.out.println("OUT OF THUMB");
                            isInsideThumb = false;
                        }
                    }
                }
            }
        }
    
        public TestScrollBar() {
    
            JPanel innerPanel = new JPanel();
            innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));
            for (int i = 1; i <= 10; i++) {
                innerPanel.add(new JLabel("Label " + i));
                innerPanel.add(Box.createRigidArea(new Dimension(0, 20)));
            }
    
            JScrollPane scrollPane = new JScrollPane(innerPanel);
            scrollPane.setPreferredSize(new Dimension(innerPanel.getPreferredSize().width, innerPanel.getPreferredSize().height / 2));
    
            scrollPane.getVerticalScrollBar().setUI(new CustomScrollBarUI());
    
            this.setLayout(new BorderLayout());
            this.add(Box.createRigidArea(new Dimension(0, 20)), BorderLayout.NORTH);
            this.add(Box.createRigidArea(new Dimension(0, 20)), BorderLayout.SOUTH);
            this.add(Box.createRigidArea(new Dimension(20, 0)), BorderLayout.EAST);
            this.add(Box.createRigidArea(new Dimension(20, 0)), BorderLayout.WEST);
            this.add(scrollPane, BorderLayout.CENTER);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    TestScrollBar frame = new TestScrollBar();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.