mouse wheel listener not working in jscrollbar
Asked Answered
E

3

3

I am developing a desktop application in netbeans platform (netbeans module) in which i have a desktoppane and a jscrollbar. i have implemented MouseWheelListener and added

scrollBar.addMouseWheelListener(this);

in the constructor of the class. now when i am scrolling the wheel of mouse it do not scroll the scroll bar though i am getting values in the

 private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) { 


 System.out.println("mouse value is------------ " + evt.paramString());
}

ouput of above sout is

mouse value is------------ MOUSE_WHEEL,(8,49),absolute(0,0),button=0,clickCount=0,scrollType=WHEEL_UNIT_SCROLL,scrollAmount=3,wheelRotation=1

what should i do now to enable mosue wheel event on jscrollbar?

I have searched but i found events for scrollpane but i am looking for scrollbar explicitly..

i have removed extra code and shown what i am looking for in the following sample code

    public final class ScrollableWindow1TopComponent extends TopComponent implements ComponentListener, MouseWheelListener {

    private javax.swing.JScrollBar scrollBar;
    private javax.swing.JDesktopPane scrollableGraphnewContainer;

    public ScrollableWindow1TopComponent() {
      this.addComponentListener(this);
      scrollBar.addMouseWheelListener(this);

    }
     private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                          
       System.out.println("mouse value is------------ " + evt.paramString());
    }
     private void scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { 
      //code that works fine 
    }
  }
Electret answered 25/4, 2012 at 12:23 Comment(4)
For better help sooner, please include an sscce.Wernher
"here is an example code" For better help sooner, post an SSCCE.Grieco
And please take more care with capitalizing J2SE class names correctly. I was about to edit that mess when I realized my time would be better spent helping other people.Grieco
Small snippets of code can sometimes help us find a solution for you, but if no answer is forthcoming soon, I urge you to create and post an sscce. It really depends on how important this is to you.Todd
E
1

my actual problem was that i was not getting the scroll value changed on mouse wheel change but i was getting the mouse wheel event so i have solved as follow:

 private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                          
        if (evt.getUnitsToScroll() > 0) {
            scrollBar.setValue(scrollBar.getValue() + 1);
        } else {
            scrollBar.setValue(scrollBar.getValue() - 1);
        }
   }      

everytime i scroll wheel up side evt.getUnitsToScroll() gives positive value and for down it gives negative value so i incremented the value of scrollbar and rest of thing are automatically handled by

  private void scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { 
     //my code...
}
Electret answered 26/4, 2012 at 7:28 Comment(0)
V
7

I think that you have look at AdjustmentListener rather than MouseXxxListener,

final JScrollBar scrollBar = myScrollpane.getVerticalScrollBar();
scrollBar.addAdjustmentListener(new AdjustmentListener() {

    @Override
    public void adjustmentValueChanged(AdjustmentEvent e) {
        System.out.println("JScrollBar's current value = " + scrollBar.getValue());
    }
});

.

.

EDIT

:-) no, without any issue on my side :-)

enter image description here

import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.*;

public class ListPanel extends JFrame {

    private static final long serialVersionUID = 1L;

    public ListPanel() {
        setLayout(new GridLayout(0, 2, 10, 10));
        DefaultListModel model = new DefaultListModel();
        model.addElement(createButtons("one"));
        model.addElement(createButtons("two"));
        model.addElement(createButtons("three"));
        model.addElement(createButtons("four"));
        model.addElement(createButtons("five"));
        model.addElement(createButtons("six"));
        model.addElement(createButtons("seven"));
        model.addElement(createButtons("eight"));
        model.addElement(createButtons("nine"));
        model.addElement(createButtons("ten"));
        model.addElement(createButtons("eleven"));
        model.addElement(createButtons("twelwe"));
        JList list = new JList(model);
        list.setCellRenderer(new PanelRenderer());
        JScrollPane scroll1 = new JScrollPane(list);
        final JScrollBar scrollBar = scroll1.getVerticalScrollBar();
        scrollBar.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                System.out.println("JScrollBar's current value = " + scrollBar.getValue());
            }
        });
        add(scroll1);
        JScrollPane scroll2 = new JScrollPane(createPanel());
        add(scroll2);        
        final JScrollBar scrollBar1 = scroll2.getVerticalScrollBar();
        scrollBar1.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                System.out.println("JScrollBar's current value = " + scrollBar1.getValue());
            }
        });

    }

    public static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1, 1, 1));
        panel.add(createButtons("one"));
        panel.add(createButtons("two"));
        panel.add(createButtons("three"));
        panel.add(createButtons("four"));
        panel.add(createButtons("five"));
        panel.add(createButtons("six"));
        panel.add(createButtons("seven"));
        panel.add(createButtons("eight"));
        panel.add(createButtons("nine"));
        panel.add(createButtons("ten"));
        panel.add(createButtons("eleven"));
        panel.add(createButtons("twelwe"));
        return panel;
    }

    public static JButton createButtons(String text) {
        JButton button = new JButton(text);
        return button;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ListPanel frame = new ListPanel();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                //frame.pack();
                frame.setSize(270, 200);
                frame.setVisible(true);
            }
        });
    }

    class PanelRenderer implements ListCellRenderer {

        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JButton renderer = (JButton) value;
            renderer.setBackground(isSelected ? Color.red : list.getBackground());
            return renderer;
        }
    }
}

output from System.out.println(...)

run:
JScrollBar's current value = 0
JScrollBar's current value = 0
JScrollBar's current value = 1
JScrollBar's current value = 2
JScrollBar's current value = 3
JScrollBar's current value = 4
JScrollBar's current value = 5
JScrollBar's current value = 6
JScrollBar's current value = 7
JScrollBar's current value = 8
JScrollBar's current value = 9
JScrollBar's current value = 10
JScrollBar's current value = 11
JScrollBar's current value = 12
JScrollBar's current value = 13
JScrollBar's current value = 14
JScrollBar's current value = 15
JScrollBar's current value = 78
JScrollBar's current value = 143
JScrollBar's current value = 78
JScrollBar's current value = 0
JScrollBar's current value = 78
JScrollBar's current value = 143
BUILD SUCCESSFUL (total time: 10 seconds)
Verruca answered 25/4, 2012 at 12:34 Comment(5)
thanks mkorbel i have tried this but it do not print the System.out.println("JScrollBar's current value = " + scrollBar.getValue());Electret
@Hemant: note that mKorbel had to create his own SSCCE to try to resolve your issue. Next time, please save him the trouble and create and post one in your question. 1+ to mKorbel for the effort.Todd
Thanks @Verruca for your SSCCE but i think i am not able to see the scrollbar moving on my mouse wheel move can you please help in that ?Electret
@HovercraftFullOfEels i will provide SSCCE next timeElectret
@Hemant Metalia remove all listeners that creating any mess, no battery included in my magic globe, really for brtter help sooner edit your question with SSCCE, because this listener works as API describedVerruca
R
2

Had you tried adding setWheelScrollingEnabled(true/false), on your JScrollPane.

Retain answered 25/4, 2012 at 14:2 Comment(1)
Ahha, that's the confusion part, since I had never worked directly on the JScrollBar, since the Java Docs, always more inclined towards using JScrollPane and then through it inturn change the behaviour of the JScrollBar.Retain
E
1

my actual problem was that i was not getting the scroll value changed on mouse wheel change but i was getting the mouse wheel event so i have solved as follow:

 private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                          
        if (evt.getUnitsToScroll() > 0) {
            scrollBar.setValue(scrollBar.getValue() + 1);
        } else {
            scrollBar.setValue(scrollBar.getValue() - 1);
        }
   }      

everytime i scroll wheel up side evt.getUnitsToScroll() gives positive value and for down it gives negative value so i incremented the value of scrollbar and rest of thing are automatically handled by

  private void scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { 
     //my code...
}
Electret answered 26/4, 2012 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.