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

JSlider needs customizable value label in GTK+ L&F

    XMLWordPrintable

Details

    Description

      A DESCRIPTION OF THE REQUEST :
      In the GTK+ look and feel, JSliders show their current value above the slider thumb. This undermines my ability to abstract the JSlider's function.

      One possible fix to this is the addition of a JSlider.setValuePainted(boolean) method.

      Another possible fix is the addition of a JSlider.setValueFormat(NumberFormat) method. (Please do not make the atrocious mistake of accepting only a DecimalFormat pattern string like JSpinner.NumberEditor does.)


      JUSTIFICATION :
      I am using a JSlider to represent a value from 0.0 to 1.0. Normally I scale the JSlider's actual value and place the result in an adjacent JLabel, but in the GTK+ look and feel, I cannot hide the integer value of the JSlider. The result is ugly and bizarre: a JSlider with "50" over its thumb, next to a JLabel containing "0.50".


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect the JSlider to not automatically paint its value, simply because that has been its behavior in the past. I can accept this new functionality, as long as I have some control over it.

      ACTUAL -
      There appears to be no way to control the appearance or format of the JSlider's value in the GTK+ look and feel.


      ---------- BEGIN SOURCE ----------
      import java.text.*;
      import java.awt.*;
      import javax.swing.*;
      import javax.swing.event.*;

      public class SliderDemo
      {
          public static void main(String[] args)
          {
              final NumberFormat valueFmt = new DecimalFormat("0.00");

              try
              {
                  UIManager.setLookAndFeel(
                      "com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
              }
              catch (Exception e)
              {
                  e.printStackTrace();
                  System.exit(1);
              }

              final JSlider slider2 = new JSlider(0, 100, 50);
              final JLabel label2 = new JLabel(valueFmt.format(0.5));
              slider2.addChangeListener(new ChangeListener()
              {
                  public void stateChanged(ChangeEvent event)
                  {
                      label2.setText(valueFmt.format(slider2.getValue() / 100.0));
                  }
              });

              try
              {
                  UIManager.setLookAndFeel(
                      "javax.swing.plaf.metal.MetalLookAndFeel");
              }
              catch (Exception e)
              {
                  e.printStackTrace();
                  System.exit(1);
              }

              final JSlider slider1 = new JSlider(0, 100, 50);
              final JLabel label1 = new JLabel(valueFmt.format(0.5));
              slider1.addChangeListener(new ChangeListener()
              {
                  public void stateChanged(ChangeEvent event)
                  {
                      label1.setText(valueFmt.format(slider1.getValue() / 100.0));
                  }
              });

              Box box1 = Box.createHorizontalBox();
              box1.add(slider1);
              box1.add(Box.createHorizontalStrut(6));
              box1.add(label1);

              Box box2 = Box.createHorizontalBox();
              box2.add(slider2);
              box2.add(Box.createHorizontalStrut(6));
              box2.add(label2);

              JPanel panel = new JPanel(new GridLayout(1, 0, 12, 0));
              panel.add(box1);
              panel.add(box2);

              panel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));

              JFrame frame = new JFrame("Slider Demo");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frame.getContentPane().add(panel);
              frame.pack();
              frame.setLocationRelativeTo(frame.getOwner());
              frame.setVisible(true);
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      One could, conceivably, forcibly set a SliderUI from a look and feel other than GTK+, if one doesn't mind the visual incongruity.

      Attachments

        Activity

          People

            stayer Kirill Kirichenko (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: