如何在鼠标右键单击时禁用JSlider的位置更改

时间:2012-03-16 11:13:48

标签: java swing awt core jslider

我在我的应用程序中使用了JSlider。我只想左键单击幻灯片JSlider,我想禁用JSlider的右键单击。我能够在右键单击时获得事件,但是滑块正在将其值更改为其他位置。

jSlider.addChangeListener(new ChangeListener() {

    public void stateChanged(ChangeEvent e) {
        //code
    }
});

jSlider.addMouseListener(new MouseAdapter() {

    public void mouseReleased(MouseEvent e) {

        if (e.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            //code
        } else {
            //code
        }

    }
});

3 个答案:

答案 0 :(得分:4)

我使用的是JDK1.7.0_03,Windows 7

UIManager.put("Slider.onlyLeftMouseButtonDrag", Boolean.TRUE);

JDK1.6.0_31:

Bug ID: 6614972 JSlider value should not change on right-click

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.metal.MetalSliderUI;

public class SliderOnlyLeftMouseButtonDragTest {
  public JComponent makeUI() {
    JSlider slider00 = new JSlider(0,10,5);
    JSlider slider01 = new JSlider(0,10,5);
    slider01.setUI(new MetalSliderUI() {
      protected TrackListener createTrackListener(JSlider slider) {
        return new TrackListener() {
          @Override public void mousePressed(MouseEvent e) {
            if(!SwingUtilities.isLeftMouseButton(e)) {
              return;
            }
            super.mousePressed(e);
          }
        };
      }
    });
    JPanel p = new JPanel();
    p.add(slider00);
    p.add(slider01);
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new SliderOnlyLeftMouseButtonDragTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

答案 1 :(得分:1)

解决ui监听器中的错误的另一种方法是

  • 找到听众
  • 实现一个包装器,只在适当时委托给原件(这里:不是右键)
  • 删除原文并添加包装

与@ aterai的方法相比,它的优势在于它独立于LAF,即您可以以完全相同的方式为每个应用它,而不是为每个子类化ui委托。

代码如下:

MouseListener[] listeners = slider.getMouseListeners();
MouseListener uiListener = null;
for (MouseListener l : listeners) {
    LOG.info(l.getClass().getName());
    if (l.getClass().getName().contains("Track")) {
        uiListener = l; 
        break;
    }
}

final MouseListener delegate = uiListener;
MouseListener wrapper = new MouseListener() {

    @Override
    public void mouseClicked(MouseEvent e) {
        if (filter(e)) return;
        delegate.mouseClicked(e);
    }

    private boolean filter(MouseEvent e) {
        return SwingUtilities.isRightMouseButton(e);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (filter(e)) return;
        delegate.mousePressed(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (filter(e)) return;
        delegate.mouseReleased(e);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        delegate.mouseEntered(e);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        delegate.mouseExited(e);
    }

};
slider.removeMouseListener(uiListener);
slider.addMouseListener(wrapper);

答案 2 :(得分:0)

I Case他没有使用1.7 首先,您必须从滑块中删除所有mouseListeners

for (MouseListener ml : slider.getMouseListeners()){
    slider.removeMouseListener(ml);
}

之后,您可以实现自己的mouseListener并将其添加到滑块。在您的mouselistener中,您必须实施计算以设置滑块的值...例如......好像

public void mouseReleased(MouseEvent e) {

    if (e.getButton() == java.awt.event.MouseEvent.BUTTON3) {
        //code whatever should happen on right click
    } else {
        e.getX()*slider.getMaximum()/slider.getWidth();
    }

}

我想由于四舍五入等原因可以改善计算。但是这个想法应该会让你成功