摇摆鼠标滚动不会滚动下拉菜单

时间:2018-04-04 08:09:48

标签: java swing events scrollbar mouseevent

我正在尝试向下滚动一个下拉框。调用Altough mouseWheelListener,不移动scrollBar,并在scrool事件后关闭下拉框。问题是在事件处理程序中设置后,scrollBar的值不会更改,它始终为0.

以下是代码的一部分:

public MyClas extends JPopupMenu implements AdjustmentListener, ContainerListener, ChangeListener{
  public MyClass(){
    .....
    setLayout(new ScrollablePopupMenuLayout(this, defaultWidth));
    JScrollBar scrollBar = new JScrollBar(JScrollBar.VERTICAL);
    scrollBar.setFocusable(false);
    scrollBar.addAdjustmentListener(this);
    addContainerListener(this);
    addMouseWheelListener(new MouseWheelListener(){
         // Here is invoked after scrolling
         public void mouseWheelMoved(MouseWheelEvent e){
         {
              int x = 1;
              scrollBar.setValue(scrollBar.getValue() + x)
         }

    });
}

1 个答案:

答案 0 :(得分:0)

在将以下行添加到mouseWheelEvent方法后,它起作用了:

e.consume();