Change display labels for JSlider?

I have a JSlider with a minus 0 and a maximum of 10000. I have a basic mark of 1000. If I were to draw shortcuts now, they would display as 0, 1000, 2000, 3000, 4000, etc. What I would like to show would be 0, 1, 2, 3, 4, 5, etc. What would be a good way to accomplish this task?

+3
source share
2 answers

using JSlider.setLabelTable (Dictionary)

EDIT

Alternatively, you can rely on the predefined shortcut user interface and just change the shortcut 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));          
    }
+2
source

JSlider.setLabelTable(Dictionary) , -; .

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);

( .)

+1

Source: https://habr.com/ru/post/1712562/


All Articles