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

JSlider's thumb moves in the wrong direction when used as a JTable cell editor.

XMLWordPrintable

    • b03
    • x86
    • windows_xp
    • Verified

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


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      When I use a JSlider as a renderer and editor in a JTable, the first time I
      click in a region to the left of the slider's thumb, it moves to the right,
      instead of moving to the left towards the mouse pointer.

      This happens only for the first click. After that the slider behaves normally.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the attached code. On the table in any of the slider cells, clickin a region to the left of any slider's thumb. The first time you do this, the thumb moves to the right, instead of to the left. After this first time, the slider moves in the correct direction towards the mouse click..


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /* Copyright 2005 The MathWorks, Inc. */
      package Prototypes;

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.event.*;
      import javax.swing.table.*;

      public class BlockParameterPanel2 extends JPanel {
        private static final long serialVersionUID = 24362462L;

        public BlockParameterPanel2() {
          JScrollPane scroll = new JScrollPane( new ParameterTable() );
          this.setLayout( new BorderLayout() );
          this.add(scroll, BorderLayout.CENTER);
        }

        private class SliderRenderer extends JSlider implements TableCellRenderer
      {
          private static final long serialVersionUID = 24362462L;

          public SliderRenderer() {
            super(0,1000);
          }

          public Component getTableCellRendererComponent(JTable table, Object
      value,
                                                         boolean isSelected,
                                                         boolean hasFocus,
                                                         int row, int col) {
            int val = ((Integer)value).intValue();
            setValue(val);

            // Needed to properly paint the slider.
            updateUI();

            return this;
          }
        }


        private class SliderEditor extends AbstractCellEditor implements
      TableCellEditor {
          private static final long serialVersionUID = 24362462L;
          private SliderRenderer renderer = new SliderRenderer();

          public Component getTableCellEditorComponent(JTable table, Object value,
                                                       boolean isSelected,
                                                       int row, int col) {
            int val = ((Integer)value).intValue();
            renderer.setValue(val);
            return renderer;
          }

          public SliderEditor() {
            renderer.addChangeListener( new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                  if (!renderer.getValueIsAdjusting()) {
                    stopCellEditing();
                  }
                }
              });
          }

          public Object getCellEditorValue() {
            return new Integer(renderer.getValue());
          }

        }


        private class ParameterTable extends JTable {
          private static final long serialVersionUID = 24362462L;

          public ParameterTable() {
            super( new Object[][] {
              { "A", new Integer(100)},
              { "B", new Integer(500)},
              { "C", new Integer(800)} } ,
                   new String[]{ "Parameter", "Value"});

            SliderRenderer renderer = new SliderRenderer();
            SliderEditor editor = new SliderEditor();

            Dimension ps = ((JComponent)renderer).getPreferredSize();
            setRowHeight(ps.height);

            getColumnModel().getColumn(1).setCellRenderer(renderer);
            getColumnModel().getColumn(1).setCellEditor(editor);
          }
        }


        // main method for unit testing
        public static void main(String[] argv) {
          try {
            String lf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
            UIManager.setLookAndFeel( lf );
          } catch(Exception exc) { }

          BlockParameterPanel2 panel = new BlockParameterPanel2();
          JFrame f = new JFrame();
          f.setBounds(100, 100, 600, 300);
          f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          f.getContentPane().add(panel);
          f.setVisible(true);

          f.addWindowListener(new WindowAdapter() {
              public void windowClosed(WindowEvent e) {
                System.exit(0);
              }
            });
        }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      In the SliderEditor constructor, adding the following lines solves the problem.
      .....
       public SliderEditor() {
          // Add these lines
          JFrame testframe = new JFrame();
           testframe.getContentPane().add(renderer);
           testframe.pack();
           testframe.getContentPane().removeAll();
           testframe.dispose();
      ......

      The issue seems to be related to the JSlider not having properly set size/location properties.
      The pack() command sets those properties and then this slider can be used as the editor component.

            mlapshin Mikhail Lapshin (Inactive)
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: