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

JScrollBar setModel breaks JScrollPane view tracking.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • client-libs
    • beta
    • generic
    • generic



      Name: boT120536 Date: 12/14/2000


      .
      --------------
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      After setting the model of a JScrollPane's JScrollBar the scroll pane will
      no longer update the view when the scroll bar is moved. This doesn't happen
      if the model is set before the scroll bar is installed in the pane.

      The following code demonstrates the problem:

      /*
       * JScrollPaneTest.java
       *
       * Created on 31 July 2000, 11:14
       */
       
      package test;

      import java.awt.event.*;
      import java.io.*;

      import javax.swing.*;

      /**
       * JScrollPane bug demo.
       *
       * @author William
       * @version $Revision$
       */

      public class JScrollPaneTest extends JScrollPane
      {
         private JTextArea textArea;
         private OutputStream out;
         
         /**
          * Creates a new JScrollPaneTest
          */
         
         public JScrollPaneTest()
         {
            textArea = new JTextArea();
            out = new MyOutputStream();
            
            final LockedBoundedRangeModel lockModel = new LockedBoundedRangeModel();
            final AbstractButton lockButton = new JToggleButton("!");
            lockButton.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                  lockModel.setLock(lockButton.isSelected());
               }
            });
            lockButton.setFocusPainted(false);
            lockButton.setContentAreaFilled(false);
            lockButton.setBorder(null);
            lockButton.setSelected(true);

            //
            // The following line breaks this scroll pane.
            //
            
            getVerticalScrollBar().setModel(lockModel);
            
            setViewportView(textArea);
            setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
            setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
            setCorner(LOWER_RIGHT_CORNER, lockButton);
         }
         
         public void test()
         {
            for (int i = 0; ; i++)
            {
               try {
                  out.write(Integer.toBinaryString(i).getBytes());
                  out.write((int) '\n');
                  Thread.sleep(50);
               } catch (Exception ex) {}
            }
         }
         
         private class LockedBoundedRangeModel extends DefaultBoundedRangeModel
         {
            private boolean lock = true;

            public boolean getLock()
            {
               return lock;
            }

            public void setLock(boolean lock)
            {
               this.lock = lock;
               if (lock)
                  setValue(getMaximum() - getExtent());
            }

            /**
             * Overridden to set the value to its maximum, unless <i>adjusting</i> is
             * true.
             */

            public void setRangeProperties(int newValue, int newExtent, int newMin,
      int newMax, boolean adjusting)
            {
               if (!adjusting && lock)
                  newValue = newMax - newExtent;
               super.setRangeProperties(newValue, newExtent, newMin, newMax,
      adjusting);
            }
         }
         
         private class MyOutputStream extends OutputStream
         {
            public void write(int b) throws IOException
            {
               textArea.append("" + (char) b);
            }
         }
         
         public static void main(String[] argv)
         {
            JFrame frame = new JFrame("Test");
            JScrollPaneTest test = new JScrollPaneTest();
            frame.getContentPane().add(test);
            frame.setBounds(50, 50, 400, 200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.show();
            test.test();
         }
      }
      (Review ID: 107796)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: