Question

Let's say I have a horizoltal JSlider from 0 to 100. What i want to do is: - set major spacing to 10 and minor to 5 but the digits shouldn't be visible - the values of extremities (0 and 100) should be visible - when I move the positioning arrow, I want to have a label next to the mouse cursor that tells me the actual value.

Was it helpful?

Solution

set major spacing to 10 and minor to 5 but the digits shouldn't be visible - the values of extremities (0 and 100) should be visible

This can be easily done using some methods of JSlider like,

JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 0);
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
Hashtable<Integer, JLabel> labels = new Hashtable<Integer, JLabel>();
labels.put(0, new JLabel("0"));
labels.put(100, new JLabel("100"));
slider.setLabelTable(labels);
slider.setPaintLabels(true);

when I move the positioning arrow, I want to have a label next to the mouse cursor that tells me the actual value.

You can achieve similar thing by using customer sliderUI and overriding it's paintThumb method. This is not exactly you want but you can try this:

slider.setUI(new BasicSliderUI(slider) {
    public void paintThumb(Graphics g) {
        super.paintThumb(g);
        g.setColor(Color.RED);
        g.drawString(slider.getValue() + "", thumbRect.x, thumbRect.y + thumbRect.height);
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top