Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5012804

JSlider value does not (re)snap to tick when value set nearby tick

XMLWordPrintable

      Name: rl43681 Date: 03/12/2004


      FULL PRODUCT VERSION :
      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

      FULL OS VERSION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      When snapToTicks is set to true, a JSlider does not re-snap to the tick when the value is set to a value nearby the current tick. This means you can have a slider with 3 ticks corresponding to values (0, 50, 100) whose value is for instance 49 or 52, even though it should snap to the tick corresponding to value 50 (and hence adjust its value to 50) in that case.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      In the provided test case, click(!) somewhere on the slider's knob, but ensure you don't actually drag the knob. The value 'jumps' to the corresponding value, and remains that value. When testing against 1.3.1 this jump also occurs, but is reset when the mouse is released and the slider snaps back to the tick.

      Note that in both cases (1.4.2 as well as 1.3.1) the knob itself does not actually move. Only the slider's value is adjusted.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The 'value jump' should occur, but only during the time the mouse is pressed. When the mouse is released the slider (and hence the value) should snap to the tick.
      ACTUAL -
      Value remains a value that does not correspond to any of the ticks. The 'value jump' is permanent.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;

      import javax.swing.*;
      import javax.swing.event.*;

      /**
       * @author Remco de Boer
       */
      public class TestCase extends MouseAdapter implements ChangeListener
      {
        private JFrame mainFrame;
        private JLabel output = new JLabel();
        private JSlider slider = new JSlider();

        public TestCase()
        {
          slider.setMajorTickSpacing((slider.getMaximum() - slider.getMinimum()) / 2);
          slider.setPaintTicks(true);
          slider.setSnapToTicks(true);
          slider.addChangeListener(this);
          slider.addMouseListener(this);

          output = new JLabel(Integer.toString(slider.getValue()));

          mainFrame = new JFrame("mainFrame");
          Container pane = mainFrame.getContentPane();
          pane.setLayout(new BorderLayout());
          pane.add(slider, BorderLayout.NORTH);
          pane.add(output, BorderLayout.SOUTH);
          mainFrame.pack();
          mainFrame.setBounds(200, 200, 150, 150);
          mainFrame.setVisible(true);
          
        }

        public static void main(String[] args)
        {
          new TestCase();
        }

        public void stateChanged(ChangeEvent e)
        {
          output.setText(Integer.toString(slider.getValue()));
        }

        public void mousePressed(MouseEvent e)
        {
          // Sets the sliderbar position to the clicked value
           int position =
            e.getX()
              * (slider.getMaximum() - slider.getMinimum())
              / slider.getWidth();

          slider.setValue((int)position);
        }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      none found
      (Incident Review ID: 216237)
      ======================================================================

            rupashka Pavel Porvatov (Inactive)
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: