问题
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
}
}
回答1:
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 :-)
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)
回答2:
Had you tried adding setWheelScrollingEnabled(true/false), on your JScrollPane.
回答3:
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...
}
来源:https://stackoverflow.com/questions/10315536/mouse-wheel-listener-not-working-in-jscrollbar