Code
class SliderPopupListener extends MouseAdapter {
private final JWindow toolTip = new JWindow();
private final JLabel label = new JLabel("", SwingConstants.CENTER);
private final Dimension size = new Dimension(30, 20);
private int prevValue = -1;
protected SliderPopupListener() {
super();
label.setOpaque(false);
label.setBackground(UIManager.getColor("ToolTip.background"));
label.setBorder(UIManager.getBorder("ToolTip.border"));
toolTip.add(label);
toolTip.setSize(size);
}
protected void updateToolTip(MouseEvent me) {
JSlider slider = (JSlider) me.getComponent();
int intValue = (int) slider.getValue();
if (prevValue != intValue) {
label.setText(String.format("%03d", slider.getValue()));
Point pt = me.getPoint();
pt.y = -size.height;
SwingUtilities.convertPointToScreen(pt, me.getComponent());
pt.translate(-size.width / 2, 0);
toolTip.setLocation(pt);
}
prevValue = intValue;
}
@Override public void mouseDragged(MouseEvent me) {
updateToolTip(me);
}
@Override public void mousePressed(MouseEvent me) {
if (UIManager.getBoolean("Slider.onlyLeftMouseButtonDrag")
&& SwingUtilities.isLeftMouseButton(me)) {
toolTip.setVisible(true);
updateToolTip(me);
}
}
@Override public void mouseReleased(MouseEvent me) {
toolTip.setVisible(false);
}
@Override public void mouseWheelMoved(MouseWheelEvent e) {
JSlider s = (JSlider) e.getComponent();
int i = (int) s.getValue() - e.getWheelRotation();
BoundedRangeModel m = s.getModel();
s.setValue(Math.min(Math.max(i, m.getMinimum()), m.getMaximum()));
}
}
References
No comments:
Post a Comment