1

http://s019.radikal.ru/i626/1203/ae/8420ef7757f7.png

    JScrollPane.getVerticalScrollBar().addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
                    System.out.println("mouseClicked");
        }
        public void mousePressed(MouseEvent e) {
                    System.out.println("mousePressed");
        }
        public void mouseReleased(MouseEvent e) {
                    System.out.println("mouseReleased");
        }
    });

It works if I click on the strip, but does not work when I click on the buttons

fabian
  • 80,457
  • 12
  • 86
  • 114
user1221483
  • 431
  • 2
  • 7
  • 20
  • there I can9t see resons for why, for better help sooner edit your question with a [SSCCE](http://sscce.org/) – mKorbel Mar 25 '12 at 21:00
  • Would listening for changes to the underlying model of the `JScrollBar` suit your needs? – Jeffrey Mar 25 '12 at 21:01
  • I do not know, I need to define pressing these buttons, please give a working example – user1221483 Mar 25 '12 at 21:05
  • @user1221483 Pressing those buttons is already defined for you. When you press the button, the `JScrollPane` scrolls. – Jeffrey Mar 25 '12 at 21:08
  • @user1221483 my curiosity, pleasy why MouseListener, all mouse events are correctly implemented in the API, – mKorbel Mar 25 '12 at 21:26

1 Answers1

1

The buttons are defined in the JScrollBar's UI so you need to extend the default UI implementation. Of course it is platform dependent. In my example I'll show you how to do it with BasicScrollBarUI. You can define a custom JScrollBar by calling the JScrollPane.setVerticalScrollBar(new CustomScrollBar()); In your CustomScrollBar you can do the following:

public class CustomScrollBar extends JScrollBar {
    public CustomScrollBar() {
        setUI(new CustomUI());
    }
    class CustomUI extends BasicScrollBarUI {
        @Override
        protected void installListeners() {
            super.installListeners();
            if (incrButton != null) {
                incrButton.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        //Increment button is clicked!
                    }
                });
            }
            if (decrButton != null) {
                decrButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        //Decrement button is clicked!
                    }
               });
            }
        }
    }
}

I've tested it under XP but without a JScrollPane. I hope it helps!

onlyhuman
  • 381
  • 3
  • 10