Package javax.swing

Examples of javax.swing.JScrollBar$ModelListener


        return handler;
    }

    protected void syncScrollPaneWithViewport() {
        JViewport viewport = scrollpane.getViewport();
        JScrollBar vsb = scrollpane.getVerticalScrollBar();
        JScrollBar hsb = scrollpane.getHorizontalScrollBar();
        JViewport rowHead = scrollpane.getRowHeader();
        JViewport colHead = scrollpane.getColumnHeader();
        boolean ltr = scrollpane.getComponentOrientation().isLeftToRight();

        if (viewport != null) {
            Dimension extentSize = viewport.getExtentSize();
            Dimension viewSize = viewport.getViewSize();
            Point viewPosition = viewport.getViewPosition();

            if (vsb != null) {
                int extent = extentSize.height;
                int max = viewSize.height;
                int value = Math.max(0, Math.min(viewPosition.y, max - extent));
                vsb.setValues(value, extent, 0, max);
            }

            if (hsb != null) {
                int extent = extentSize.width;
                int max = viewSize.width;
                int value;

                if (ltr) {
                    value = Math.max(0, Math.min(viewPosition.x, max - extent));
                } else {
                    int currentValue = hsb.getValue();

                    /*
                     * Use a particular formula to calculate "value" until
                     * effective x coordinate is calculated.
                     */
                    if (setValueCalled && ((max - currentValue) == viewPosition.x)) {
                        value = Math.max(0, Math.min(max - extent, currentValue));
                        /*
                         * After "extent" is set, turn setValueCalled flag off.
                         */
                        if (extent != 0) {
                            setValueCalled = false;
                        }
                    } else {
                        if (extent > max) {
                            viewPosition.x = max - extent;
                            viewport.setViewPosition(viewPosition);
                            value = 0;
                        } else {
                            /*
                             * The following line can't handle a small value of
                             * viewPosition.x like Integer.MIN_VALUE correctly
                             * because (max - extent - viewPositoiin.x) causes
                             * an overflow. As a result, value becomes zero.
                             * (e.g. setViewPosition(Integer.MAX_VALUE, ...) in
                             * a user program causes a overflow. Its expected
                             * value is (max - extent).) However, this seems a
                             * trivial bug and adding a fix makes this
                             * often-called method slow, so I'll leave it until
                             * someone claims.
                             */
                            value = Math.max(0, Math.min(max - extent, max - extent - viewPosition.x));
                        }
                    }
                }
                hsb.setValues(value, extent, 0, max);
            }

            if (rowHead != null) {
                Point p = rowHead.getViewPosition();
                p.y = viewport.getViewPosition().y;
View Full Code Here


    private void updateVerticalScrollBar(PropertyChangeEvent pce) {
        updateScrollBar(pce, vsbChangeListener, vsbPropertyChangeListener);
    }

    private void updateScrollBar(PropertyChangeEvent pce, ChangeListener cl, PropertyChangeListener pcl) {
        JScrollBar sb = (JScrollBar) pce.getOldValue();
        if (sb != null) {
            if (cl != null) {
                sb.getModel().removeChangeListener(cl);
            }
            if (pcl != null) {
                sb.removePropertyChangeListener(pcl);
            }
        }
        sb = (JScrollBar) pce.getNewValue();
        if (sb != null) {
            if (cl != null) {
                sb.getModel().addChangeListener(cl);
            }
            if (pcl != null) {
                sb.addPropertyChangeListener(pcl);
            }
        }
    }
View Full Code Here

        // MouseWheelListener
        //
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (scrollpane.isWheelScrollingEnabled() && e.getWheelRotation() != 0) {
                boolean isHorizontal = (e.getModifiersEx() & MouseWheelEvent.SHIFT_DOWN_MASK) != 0;
                JScrollBar toScroll = scrollpane.getVerticalScrollBar();
                int direction = e.getWheelRotation() < 0 ? -1 : 1;
                int orientation = SwingConstants.VERTICAL;

                // find which scrollbar to scroll, or return if none
                if (toScroll == null || !toScroll.isVisible() || isHorizontal) {
                    toScroll = scrollpane.getHorizontalScrollBar();
                    if (toScroll == null || !toScroll.isVisible()) {
                        return;
                    }
                    orientation = SwingConstants.HORIZONTAL;
                }

                if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
                    JViewport vp = scrollpane.getViewport();
                    if (vp == null) {
                        return;
                    }
                    Component comp = vp.getView();
                    int units = Math.abs(e.getUnitsToScroll());

                    // When the scrolling speed is set to maximum, it's possible
                    // for a single wheel click to scroll by more units than
                    // will fit in the visible area. This makes it
                    // hard/impossible to get to certain parts of the scrolling
                    // Component with the wheel. To make for more accurate
                    // low-speed scrolling, we limit scrolling to the block
                    // increment if the wheel was only rotated one click.
                    boolean limitScroll = Math.abs(e.getWheelRotation()) == 1;

                    // Check if we should use the visibleRect trick
                    Object fastWheelScroll = toScroll.getClientProperty("JScrollBar.fastWheelScrolling");
                    if (Boolean.TRUE == fastWheelScroll && comp instanceof Scrollable) {
                        // 5078454: Under maximum acceleration, we may scroll
                        // by many 100s of units in ~1 second.
                        //
                        // BasicScrollBarUI.scrollByUnits() can bog down the EDT
                        // with repaints in this situation. However, the
                        // Scrollable interface allows us to pass in an
                        // arbitrary visibleRect. This allows us to accurately
                        // calculate the total scroll amount, and then update
                        // the GUI once. This technique provides much faster
                        // accelerated wheel scrolling.
                        Scrollable scrollComp = (Scrollable) comp;
                        Rectangle viewRect = vp.getViewRect();
                        int startingX = viewRect.x;
                        boolean leftToRight = comp.getComponentOrientation().isLeftToRight();
                        int scrollMin = toScroll.getMinimum();
                        int scrollMax = toScroll.getMaximum() - toScroll.getModel().getExtent();

                        if (limitScroll) {
                            int blockIncr = scrollComp.getScrollableBlockIncrement(viewRect, orientation, direction);
                            if (direction < 0) {
                                scrollMin = Math.max(scrollMin, toScroll.getValue() - blockIncr);
                            } else {
                                scrollMax = Math.min(scrollMax, toScroll.getValue() + blockIncr);
                            }
                        }

                        for (int i = 0; i < units; i++) {
                            int unitIncr = scrollComp.getScrollableUnitIncrement(viewRect, orientation, direction);
                            // Modify the visible rect for the next unit, and
                            // check to see if we're at the end already.
                            if (orientation == SwingConstants.VERTICAL) {
                                if (direction < 0) {
                                    viewRect.y -= unitIncr;
                                    if (viewRect.y <= scrollMin) {
                                        viewRect.y = scrollMin;
                                        break;
                                    }
                                } else { // (direction > 0
                                    viewRect.y += unitIncr;
                                    if (viewRect.y >= scrollMax) {
                                        viewRect.y = scrollMax;
                                        break;
                                    }
                                }
                            } else {
                                // Scroll left
                                if ((leftToRight && direction < 0) || (!leftToRight && direction > 0)) {
                                    viewRect.x -= unitIncr;
                                    if (leftToRight) {
                                        if (viewRect.x < scrollMin) {
                                            viewRect.x = scrollMin;
                                            break;
                                        }
                                    }
                                }
                                // Scroll right
                                else if ((leftToRight && direction > 0) || (!leftToRight && direction < 0)) {
                                    viewRect.x += unitIncr;
                                    if (leftToRight) {
                                        if (viewRect.x > scrollMax) {
                                            viewRect.x = scrollMax;
                                            break;
                                        }
                                    }
                                } else {
                                    assert false : "Non-sensical ComponentOrientation / scroll direction";
                                }
                            }
                        }
                        // Set the final view position on the ScrollBar
                        if (orientation == SwingConstants.VERTICAL) {
                            toScroll.setValue(viewRect.y);
                        } else {
                            if (leftToRight) {
                                toScroll.setValue(viewRect.x);
                            } else {
                                // rightToLeft scrollbars are oriented with
                                // minValue on the right and maxValue on the
                                // left.
                                int newPos = toScroll.getValue() - (viewRect.x - startingX);
                                if (newPos < scrollMin) {
                                    newPos = scrollMin;
                                } else if (newPos > scrollMax) {
                                    newPos = scrollMax;
                                }
                                toScroll.setValue(newPos);
                            }
                        }
                    } else {
                        // Viewport's view is not a Scrollable, or fast wheel
                        // scrolling is not enabled.
View Full Code Here

            if (viewport != null) {
                if (e.getSource() == viewport) {
                    viewportStateChanged(e);
                } else {
                    JScrollBar hsb = scrollpane.getHorizontalScrollBar();
                    if (hsb != null && e.getSource() == hsb.getModel()) {
                        hsbStateChanged(viewport, e);
                    } else {
                        JScrollBar vsb = scrollpane.getVerticalScrollBar();
                        if (vsb != null && e.getSource() == vsb.getModel()) {
                            vsbStateChanged(viewport, e);
                        }
                    }
                }
            }
View Full Code Here

        private void sbPropertyChange(PropertyChangeEvent e) {
            String propertyName = e.getPropertyName();
            Object source = e.getSource();

            if ("model" == propertyName) {
                JScrollBar sb = scrollpane.getVerticalScrollBar();
                BoundedRangeModel oldModel = (BoundedRangeModel) e.getOldValue();
                ChangeListener cl = null;

                if (source == sb) {
                    cl = vsbChangeListener;
                } else if (source == scrollpane.getHorizontalScrollBar()) {
                    sb = scrollpane.getHorizontalScrollBar();
                    cl = hsbChangeListener;
                }
                if (cl != null) {
                    if (oldModel != null) {
                        oldModel.removeChangeListener(cl);
                    }
                    if (sb.getModel() != null) {
                        sb.getModel().addChangeListener(cl);
                    }
                }
            } else if ("componentOrientation" == propertyName) {
                if (source == scrollpane.getHorizontalScrollBar()) {
                    JScrollBar hsb = scrollpane.getHorizontalScrollBar();
                    JViewport viewport = scrollpane.getViewport();
                    Point p = viewport.getViewPosition();
                    if (scrollpane.getComponentOrientation().isLeftToRight()) {
                        p.x = hsb.getValue();
                    } else {
                        p.x = viewport.getViewSize().width - viewport.getExtentSize().width - hsb.getValue();
                    }
                    viewport.setViewPosition(p);
                }
            }
        }
View Full Code Here

          {
            scrollToCenter(true);
          }
          else
          {
            JScrollBar scrollBar = getHorizontalScrollBar();

            if (scrollBar != null)
            {
              scrollBar.setValue((scrollBar.getMaximum() / 3) - 4);
            }
          }

          if (getViewport().getHeight() > pageSize.getHeight())
          {
            scrollToCenter(false);
          }
          else
          {
            JScrollBar scrollBar = getVerticalScrollBar();

            if (scrollBar != null)
            {
              scrollBar.setValue((scrollBar.getMaximum() / 4) - 4);
            }
          }
        }
      });
    }
View Full Code Here

   *
   */
  protected void maintainScrollBar(boolean horizontal, double factor,
      boolean center)
  {
    JScrollBar scrollBar = (horizontal) ? getHorizontalScrollBar()
        : getVerticalScrollBar();

    if (scrollBar != null)
    {
      BoundedRangeModel model = scrollBar.getModel();
      int newValue = (int) Math.round(model.getValue() * factor)
          + (int) Math.round((center) ? (model.getExtent()
              * (factor - 1) / 2) : 0);
      model.setValue(newValue);
    }
View Full Code Here

  /**
   *
   */
  public void scrollToCenter(boolean horizontal)
  {
    JScrollBar scrollBar = (horizontal) ? getHorizontalScrollBar()
        : getVerticalScrollBar();

    if (scrollBar != null)
    {
      final BoundedRangeModel model = scrollBar.getModel();
      final int newValue = ((model.getMaximum()) / 2) - model.getExtent()
          / 2;
      model.setValue(newValue);
    }
  }
View Full Code Here

        if (zoomGesture)
        {
          double dx = e.getX() - start.getX();
          double w = finderBounds.getWidth();

          final JScrollBar hs = graphComponent
              .getHorizontalScrollBar();
          final double sx;

          if (hs != null)
          {
            sx = (double) hs.getValue() / hs.getMaximum();
          }
          else
          {
            sx = 0;
          }

          final JScrollBar vs = graphComponent.getVerticalScrollBar();
          final double sy;

          if (vs != null)
          {
            sy = (double) vs.getValue() / vs.getMaximum();
          }
          else
          {
            sy = 0;
          }

          mxGraphView view = graphComponent.getGraph().getView();
          double scale = view.getScale();
          double newScale = scale - (dx * scale) / w;
          double factor = newScale / scale;
          view.setScale(newScale);

          if (hs != null)
          {
            hs.setValue((int) (sx * hs.getMaximum() * factor));
          }

          if (vs != null)
          {
            vs.setValue((int) (sy * vs.getMaximum() * factor));
          }
        }

        zoomGesture = false;
        start = null;
View Full Code Here

        htmlTextArea.addHyperlinkListener(this);
        scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
       
        scrollPane.setViewportView(htmlTextArea);
        JScrollBar sb=
        scrollPane.getVerticalScrollBar();
//        sb.addAdjustmentListener(new AdjustmentListener(){
//
//      public void adjustmentValueChanged(AdjustmentEvent arg0) {
//        System.err.println("Scrolling");
//      }
//         
//        });
        sb.addMouseListener(new MouseListener(){

      public void mouseClicked(MouseEvent arg0) {}

      public void mouseEntered(MouseEvent arg0) {
        scrolling = true;
      }

      public void mouseExited(MouseEvent arg0) {
      }

      public void mousePressed(MouseEvent arg0) {
      }

      public void mouseReleased(MouseEvent arg0) {
      }
         
        });
        sb.addFocusListener(new FocusListener(){

      public void focusGained(FocusEvent arg0) {
        scrolling = true;
      }

      public void focusLost(FocusEvent arg0) {
      }
         
        });
        scrollPane.getViewport().addChangeListener(new ChangeListener(){

      public void stateChanged(ChangeEvent arg0) {
        if (!scrolling) {
          // When displaying new page, scroll to top.
          // Keep pumpng this event through
          // this must be wrong, but I can't work out the
          // right way to do this.
           scrollPane.getViewport().scrollRectToVisible(new Rectangle(-1000,-1000,1,1));
        }
        if (scrolling && currentPage==lastPage) {
          JScrollBar sb = scrollPane.getVerticalScrollBar();
//          System.err.println(sb.getValue() + " "+ sb.getMaximum() + " " + sb.getHeight());
          if (sb.getValue() + sb.getHeight() == sb.getMaximum())
            rightButton.setEnabled(true);
        }
//        System.err.println(updated+" Ch: "+arg0.toString());
//          if (updated>0 && !scrollPane.getViewport().isValid()) {
//            scrollPane.getViewport().setViewPosition(new Point());
View Full Code Here

TOP

Related Classes of javax.swing.JScrollBar$ModelListener

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.