Change displayable labels for a JSlider?

做~自己de王妃 提交于 2019-12-04 13:08:16

using JSlider.setLabelTable(Dictionary)

EDIT

Alternatively you can rely on predefined label UI and just change the label text:

    Enumeration e = jSlider.getLabelTable().keys();

    while (e.hasMoreElements()) {
        Integer i = (Integer) e.nextElement();
        JLabel label = (JLabel) jSlider.getLabelTable().get(i);
        label.setText(String.valueOf(i / 1000));          
    }

You could use JSlider.setLabelTable(Dictionary) to set specific labels for the values you wish to render differently; e.g.

JSlider slider = ...

Dictionary dict = new Hashtable();
for (int i=0; i<=10000; i += 1000) {  
  dict.put(i, new JLabel(Integer.toString(i / 1000)));
}

slider.setLabelTable(dict);

(Edited based on previous comments.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!