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

JSlider does not respond after setModel

XMLWordPrintable

    • swing1.1
    • generic
    • generic



      Name: tb29552 Date: 06/29/98


      /*
       * Here is a simple application that has a JSlider
       * and 2 JButtons. One button changes the slider
       * value. The other changes the slider model
       *
       * When the app is first started, clicking on the
       * "Value" JButton will advance the slider.
       *
       * After the "Change model" button is clicked,
       * the "Value" button continues to call setValue()
       * but the slider does not move.
       */
      /*
       * The cause is the fact that the L&F class is not a
       * PropertyChangeListener of the JComponent. When
       * setModel changes the model, the references
       * held by the L&F delegate become invalid. The L&F
       * delegates do not change their modelListeners to
       * the new model.
       *
       * Changing the L&F delegate to be a listener of the
       * JComponent instead of the model will also kill
       * this bug.
       */

      import com.sun.java.swing.*;
      import java.awt.event.*;

      public class SetModelBug extends JFrame {
        public static void main( String arg[] ) {
          SetModelBug b = new SetModelBug();
          b.setSize( 300, 50 );
          b.show();
        }

        protected SetModelBug() {
          slider = new JSlider(); // Uses default model

          // Construct the model change button
          modelChangeButton = new JButton("Change model");
          modelChangeButton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent event ) {
              if ( newModel == null ) {
                newModel = new CustomBoundedRangeModel();
                slider.setModel( newModel );
              }
            }
          });

          // Construct the value change button
          valueChangeButton = new JButton("Value");
          valueChangeButton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent event ) {
              int newValue = slider.getValue() + 10;
              if ( newValue > slider.getMaximum() )
                newValue = slider.getMinimum();
              slider.setValue( newValue );
      System.out.println("In \"Value\" JButton: setValue to: " +
      newValue);
            }
          });

          // Add components
          rootPane.getContentPane().add( slider, "Center" );
          rootPane.getContentPane().add( modelChangeButton, "West" );
          rootPane.getContentPane().add( valueChangeButton, "East" );
        }

        private JSlider slider;
        private JButton modelChangeButton;
        private JButton valueChangeButton;
        private CustomBoundedRangeModel newModel;
      }

      /**
         Custom model that fixes the extent to 0
      */
      class CustomBoundedRangeModel extends DefaultBoundedRangeModel {
        public void setRangeProperties(
                                       int newValue,
                                       int newExtent,
                                       int newMin,
                                       int newMax,
                                       boolean adjusting) {
          super.setRangeProperties( newValue, 0, newMin, newMax, adjusting );
        }
      }

      (Review ID: 34448)
      ======================================================================

            tsantossunw Tom Santos (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: