JSlider 标签中的工具提示 [英] Tooltip in labels in JSlider

查看:29
本文介绍了JSlider 标签中的工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带有标签的 JSlider,其中有一个工具提示.

I want to create a JSlider with labels, which have a tooltip.

JSlider slider = new JSlider();
JLabel label = new JLabel("First");

slider.setPaintLabels(true);
Hashtable<Integer, JLabel> labels = new Hashtable<Integer, JLabel>();

label.setToolTipText("Tooltip");

labels.put(new Integer(0), label);
slider.setLabelTable(labels);

但是,此代码不起作用.我认为是因为我们可以在 JSlider 中添加工具提示,并且它覆盖"了所有其他内容.

But, this code does not work. I think it's because we can add tooltip to JSlider, and it "covered" all others.

有什么方法可以解决我的问题吗?

Is there a method, how I can resolve my problem?

推荐答案

您需要重写 getToolTipText(MouseEvent) 方法.

然后在代码中,您需要确定鼠标在滑块中的位置来确定要显示的文本.我从未尝试过,但您可以为此使用 BasicSliderUI.它有方法 valueForXPositionvalueForYPosition 可能会有所帮助.

Then in the code you would need to determine the mouse position in the slider to determine the text to display. I've never tried it but you might be able to use the BasicSliderUI for this. It has methods valueForXPosition and valueForYPosition which might help.

这篇关于JSlider 标签中的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆