3

I have a JSlider, range 1 to 60. I have the ticks labeled every 10, starting at 10. My code for the slider is this:

JSlider slider = new JSlider(1, 60);
slider.setValue(20);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(1);
slider.setLabelTable(slider.createStandardLabels(10, 10));
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);

The behavior is basically what would be expected, looking at the code; it creates a slider with the given range, sets the value to 20, spaces major ticks at 1, 11, 21, etc., labels at 10, 20, 30, etc. However, I want the major ticks to line up with the labels at 10, 20, 30, etc., but there is no method setMajorTickSpacing(int spacing, int start).

Is there any workaround to do something equivalent to createStandardLabels(10, 10) with setMajorTickSpacing so that the major ticks line up with the labels?

Edit: My problem is with the ticks, not the labels. I want ticks created incrementally every 1 tick, major ticks (not labels) created every multiple of 10 starting on 10. Unlike Java Slider - how to make custom ticks? this tick pattern is uniform, simply not starting at the slider's minimum. If I understand correctly, setLabelTable only allows the changing of labels, not unlabeled ticks.

Community
  • 1
  • 1
17slim
  • 1,233
  • 1
  • 16
  • 21
  • use the setLabelTable method to set your custom ticks/labels – Hovercraft Full Of Eels Jul 18 '16 at 15:06
  • You'll need your own code to create and fill the Dictionary used in this method of course. – Hovercraft Full Of Eels Jul 18 '16 at 15:10
  • setLabelTable doesn't set custom ticks. In fact, it has no effect on ticks at all. I am already using it to create labels at exactly where I want them, and its behavior is exactly what I want. This question is about getting the major ticks to appear at the same values. – 17slim Jul 18 '16 at 15:20
  • It will set custom ticks if and where you place them. You're using it but you're not creating your Dictionary correctly. Create a `Dictionary` object and fill it with data at the key Integer locations **YOU** specify. – Hovercraft Full Of Eels Jul 18 '16 at 15:22
  • I manually created a `Hashtable` full of the values, and gave each integer a proper `JLabel`, but it does not create any ticks either at the labels or otherwise, it only labels the location. Perhaps the behavior has changed in newer versions of Java? I'm required to use 1.6. – 17slim Jul 18 '16 at 15:36
  • You're right. I've reopened the question. If this were my problem, I'd start looking at the source code. – Hovercraft Full Of Eels Jul 18 '16 at 18:33
  • in UIManaget is key Slider.majorTickLength with value at 6, but unfortunatelly works for me just from key PAGE UP/DOWN – mKorbel Jul 18 '16 at 19:26
  • mKorbel, I think you're going to have to rephrase, I couldn't understand that. Are you trying to ask another question? If so, do not comment on a related question, ask a formal question in SO. – 17slim Jul 18 '16 at 19:31

1 Answers1

0

I found a workaround that allows pretty much the functionality that I want:

JSlider slider = new JSlider(0, 60); //minimum=0 so major ticks appear on multiples of 10
slider.setValue(20);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(1);
slider.setLabelTable(slider.createStandardLabels(10, 10));
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);

//If the slider is set to 0, reset it to 1
slider.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
        if (((JSlider) e.getSource()).getValue() == 0)
            ((JSlider) e.getSource()).setValue(1);
    }
});

This way, the slider's value still has an effective minimum of 1, but technically has a minimum at 0 so the major ticks appear at the correct numbers. It's kind of an ugly way of doing it in my opinion, but it does give the slider the behavior I want. I will wait to accept this as the answer for a little while, just in case someone does find a better way of doing it.

17slim
  • 1,233
  • 1
  • 16
  • 21