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

Offset for JSlider ticks needed

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • 6
    • client-libs

      A DESCRIPTION OF THE REQUEST :
      My problem is that I want to use a JSlider for a GUI where users can select memory values between 32 and 512 MB. I want to have ticks and labels at the JSlider. If I just use setMajorTickSpacing(128) the ticks and labels are at very strange values (160, 288, 412). If I create my own label table (128, 256, 384, 512) the labels are at the right positions at the JSlider but the ticks are not aligned with the label table (they are still at the 160, 288, 412).

      JUSTIFICATION :
      Sliders with a minimum value different from zero and certain tick alignment requirements are very common. It is important that this situation is supported without the need to subclass UI classes.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      JSlider needs a "majorTickOffset" property/member. This property must be used while painting the ticks and labels so that the first major tick is aligned at this value. This way it is possible to create JSliders with a minimum value != 0 and reasonable tick alignment.
      ACTUAL -
      There is no "majorTickOffset" property/member and the JSlider shows either strange values or the values and the ticks are not correctly aligned.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.util.Hashtable;
      import javax.swing.*;

      public class JSliderTest extends JFrame {
          
          public JSliderTest() {
              
              int MIN = 32;
              int STEP = 128;
              int MAX = 512;
              
              GridBagConstraints gridBagConstraints;
              
              JSlider jSlider1 = new JSlider(); // aligned ticks but stupid values
              JSlider jSlider2 = new JSlider(); // good values but unaligned ticks
              
              getContentPane().setLayout(new GridBagLayout());
              
              setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              jSlider1.setMajorTickSpacing(STEP);
              jSlider1.setMaximum(MAX);
              jSlider1.setMinimum(MIN);
              jSlider1.setPaintLabels(true);
              jSlider1.setPaintTicks(true);
              jSlider1.setValue(MIN);
              gridBagConstraints = new GridBagConstraints();
              gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
              getContentPane().add(jSlider1, gridBagConstraints);
              
              jSlider2.setMajorTickSpacing(STEP);
              jSlider2.setMaximum(MAX);
              jSlider2.setMinimum(MIN);
              jSlider2.setPaintLabels(true);
              jSlider2.setPaintTicks(true);
              jSlider2.setValue(MIN);
              getContentPane().add(jSlider2, new GridBagConstraints());
              
              Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
              labelTable.put(new Integer(MIN), new JLabel(String.valueOf(MIN)));
              for (int i = STEP; i <= MAX; i += STEP) {
                  JLabel label = new JLabel(String.valueOf(i));
                  labelTable.put(new Integer(i), label);
              }
              jSlider2.setLabelTable(labelTable);
              
              pack();
          }
          
          public static void main(String[] args) {
              EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      new JSliderTest().setVisible(true);
                  }
              });
          }
      }

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

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: