Java Code Examples for javax.swing.JViewport#setViewPosition()

The following examples show how to use javax.swing.JViewport#setViewPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ETableColumn.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Forces the table to resize given column.
 */
private void resize(int newWidth, JTable table) {
    int oldWidth = getWidth();
    JTableHeader header = table.getTableHeader();
    if (header == null) {
        return;
    }
    header.setResizingColumn(this);
    final int oldMin = getMinWidth();
    final int oldMax = getMaxWidth();
    setMinWidth(newWidth);
    setMaxWidth(newWidth);
    setWidth(newWidth);
    // The trick is to restore the original values
    // after the table has be layouted. During layout this column
    // has fixed width (by setting min==max==preffered)
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            setMinWidth(oldMin);
            setMaxWidth(oldMax);
        }
    });
    Container container;
    if ((header.getParent() == null) ||
            ((container = header.getParent().getParent()) == null) ||
            !(container instanceof JScrollPane)) {
        header.setResizingColumn(null);
        return;
    }
    
    if (!container.getComponentOrientation().isLeftToRight() &&
            ! header.getComponentOrientation().isLeftToRight()) {
        if (table != null) {
            JViewport viewport = ((JScrollPane)container).getViewport();
            int viewportWidth = viewport.getWidth();
            int diff = newWidth - oldWidth;
            int newHeaderWidth = table.getWidth() + diff;
            
            /* Resize a table */
            Dimension tableSize = table.getSize();
            tableSize.width += diff;
            table.setSize(tableSize);
            
            /* If this table is in AUTO_RESIZE_OFF mode and
             * has a horizontal scrollbar, we need to update
             * a view's position.
             */
            if ((newHeaderWidth >= viewportWidth) &&
                    (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
                Point p = viewport.getViewPosition();
                p.x = Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
                viewport.setViewPosition(p);
            }
        }
    }
    header.setResizingColumn(null);
}
 
Example 2
Source File: IndexedCustomizer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** notify the Component to autoscroll */
public void autoscroll(Point cursorLoc) {
    JViewport viewport = getViewport();
    Point viewPos = viewport.getViewPosition();
    int viewHeight = viewport.getExtentSize().height;

    if ((cursorLoc.y - viewPos.y) <= realInsets.top) {
        // scroll up
        viewport.setViewPosition(new Point(viewPos.x, Math.max(viewPos.y - realInsets.top, 0)));
    } else if (((viewPos.y + viewHeight) - cursorLoc.y) <= realInsets.bottom) {
        // scroll down
        viewport.setViewPosition(
            new Point(viewPos.x, Math.min(viewPos.y + realInsets.bottom, this.getHeight() - viewHeight))
        );
    }
}
 
Example 3
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void changed() {
    JViewport viewport = sp.getViewport();
    Point viewPosition = viewport.getViewPosition();
    if (viewPosition.x > 0) {
        try {
            Rectangle textRect = editorPane.getUI().modelToView(editorPane, editorPane.getDocument().getLength());
            int textLength = textRect.x + textRect.width;
            int viewLength = viewport.getExtentSize().width;
            //System.out.println("Utilities.createSingleLineEditor(): spLength = "+sp.getSize().width+", viewLength = "+viewLength+", textLength = "+textLength+", viewPosition = "+viewPosition);
            if (textLength < (viewPosition.x + viewLength)) {
                viewPosition.x = Math.max(textLength - viewLength, 0);
                viewport.setViewPosition(viewPosition);
                //System.out.println("Utilities.createSingleLineEditor(): setting new view position = "+viewPosition);
            }
        } catch (BadLocationException blex) {
            Exceptions.printStackTrace(blex);
        }
    }
}
 
Example 4
Source File: PeriodicTasksService.java    From Open-LaTeX-Studio with MIT License 5 votes vote down vote up
private void drawPreview() {
    JPanel pdfImagePanel = pdfDisplay.drawPreviewOnJPanel();
   
    if (pdfImagePanel != null) {
        JViewport vp = jScrollPane.getViewport();
        Point p = vp.getViewPosition();
        vp.setView(pdfImagePanel);
        vp.setViewPosition(p);

    }
    
    jLabel.setText("of " + pdfDisplay.getTotalPages());
}
 
Example 5
Source File: DefenseCalculationSettingsPanel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
private void scroll() {
    Point point = new Point(0, (int) (jTextPane1.getSize().getHeight()));
    JViewport vp = jScrollPane1.getViewport();
    if ((vp == null) || (point == null)) {
        return;
    }
    vp.setViewPosition(point);
}
 
Example 6
Source File: RetimerCalculationPanel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
private void scroll() {
    Point point = new Point(0, (int) (jTextPane1.getSize().getHeight()));
    JViewport vp = jScrollPane1.getViewport();
    if ((vp == null) || (point == null)) {
        return;
    }
    vp.setViewPosition(point);
}
 
Example 7
Source File: AttackCalculationPanel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
private void scroll() {
    Point point = new Point(0, (int) (jTextPane1.getSize().getHeight()));
    JViewport vp = jScrollPane1.getViewport();
    if ((vp == null) || (point == null)) {
        return;
    }
    vp.setViewPosition(point);
}
 
Example 8
Source File: SupportRefillCalculationPanel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
private void scroll() {
    Point point = new Point(0, (int) (jTextPane1.getSize().getHeight()));
    JViewport vp = jScrollPane1.getViewport();
    if (vp == null) {
        return;
    }
    vp.setViewPosition(point);
}
 
Example 9
Source File: SeaGlassScrollPaneUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
private void hsbStateChanged(JViewport viewport, ChangeEvent e) {
    BoundedRangeModel model = (BoundedRangeModel) (e.getSource());
    Point p = viewport.getViewPosition();
    int value = model.getValue();
    if (scrollpane.getComponentOrientation().isLeftToRight()) {
        p.x = value;
    } else {
        int max = viewport.getViewSize().width;
        int extent = viewport.getExtentSize().width;
        int oldX = p.x;

        /*
         * Set new X coordinate based on "value".
         */
        p.x = max - extent - value;

        /*
         * If setValue() was called before "extent" was fixed, turn
         * setValueCalled flag on.
         */
        if ((extent == 0) && (value != 0) && (oldX == max)) {
            setValueCalled = true;
        } else {
            /*
             * When a pane without a horizontal scroll bar was reduced
             * and the bar appeared, the viewport should show the right
             * side of the view.
             */
            if ((extent != 0) && (oldX < 0) && (p.x == 0)) {
                p.x += value;
            }
        }
    }
    viewport.setViewPosition(p);
}
 
Example 10
Source File: DebuggingViewComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
    DebugTreeView tView = getTreeView();
    if (tView == null) {
        return;
    }
    JViewport viewport = tView.getViewport();
    Point position = viewport.getViewPosition();
    viewport.setViewPosition(new Point(e.getValue(), position.y));
}
 
Example 11
Source File: DebuggingViewComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void adjustTreeScrollBar(int treeViewWidth) {
    DebugTreeView tView = getTreeView();
    if (tView == null) {
        scrollBarPanel.setVisible(false);
        return;
    }
    JViewport viewport = tView.getViewport();
    Point point = viewport.getViewPosition();
    if (point.y < 0) {
        viewport.setViewPosition(new Point(point.x, 0));
    }
    Dimension viewSize = viewport.getExtentSize();
    Dimension treeSize = viewport.getViewSize();
    if (treeViewWidth < 0) {
        treeViewWidth = treeSize.width;
    }
    int unitHeight = tView.getUnitHeight();
    if (unitHeight > 0) {
        JScrollBar sbar = mainScrollPane.getVerticalScrollBar();
        if (sbar.getUnitIncrement() != unitHeight) {
            sbar.setUnitIncrement(unitHeight);
        }
    }
    if (treeViewWidth <= viewSize.width) {
        scrollBarPanel.setVisible(false);
    } else {
        treeScrollBar.setMaximum(treeViewWidth);
        treeScrollBar.setVisibleAmount(viewSize.width);
        if (unitHeight > 0) {
            treeScrollBar.setUnitIncrement(unitHeight / 2);
        }
        treeScrollBar.setBlockIncrement(viewSize.width);
        scrollBarPanel.setVisible(true);
    } // else
}
 
Example 12
Source File: HistogramDisplay.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
    final JViewport scrollpane = (JViewport) getParent();
    final Point pos = scrollpane.getViewPosition();
    final int y;
    final int SCROLL_HEIGHT = 50;
    if (e.getWheelRotation() < 0) {
        y = pos.y - (e.getScrollAmount() * SCROLL_HEIGHT);
    } else {
        y = pos.y + (e.getScrollAmount() * SCROLL_HEIGHT);
    }

    scrollpane.setViewPosition(new Point(0, Math.max(0, y)));
}
 
Example 13
Source File: AutoscrollSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Performs autoscroll operation.
*/
public void autoscroll(Point cursorLoc) {
    JViewport viewport = getViewport();

    if (viewport == null) {
        return;
    }

    Point viewPos = viewport.getViewPosition();
    int viewHeight = viewport.getExtentSize().height;
    int viewWidth = viewport.getExtentSize().width;

    // perform scrolling
    if ((cursorLoc.y - viewPos.y) < insets.top) {
        // scroll up
        viewport.setViewPosition(new Point(viewPos.x, Math.max(viewPos.y - scrollUnits.top, 0)));
    } else if (((viewPos.y + viewHeight) - cursorLoc.y) < insets.bottom) {
        // scroll down
        viewport.setViewPosition(
            new Point(viewPos.x, Math.min(viewPos.y + scrollUnits.bottom, comp.getHeight() - viewHeight))
        );
    } else if ((cursorLoc.x - viewPos.x) < insets.left) {
        // scroll left
        viewport.setViewPosition(new Point(Math.max(viewPos.x - scrollUnits.left, 0), viewPos.y));
    } else if (((viewPos.x + viewWidth) - cursorLoc.x) < insets.right) {
        // scroll right
        viewport.setViewPosition(
            new Point(Math.min(viewPos.x + scrollUnits.right, comp.getWidth() - viewWidth), viewPos.y)
        );
    }
}
 
Example 14
Source File: EditorUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void setExtentPosition(int x, int y) {
    JViewport port = getParentViewport();
    if (port != null) {
        Point p = new Point(Math.max(x, 0), Math.max(y, 0));
        port.setViewPosition(p);
    }
}
 
Example 15
Source File: PDFViewerTopComponent.java    From Open-LaTeX-Studio with MIT License 5 votes vote down vote up
private void refreshDisplayPane() {
    JPanel pdfImagePanel = pdfDisplay.drawPreviewOnJPanel();
   
    if (pdfImagePanel != null) {
        JViewport vp = jScrollPane1.getViewport();
        Point p = vp.getViewPosition();
        vp.setView(pdfImagePanel);
        vp.setViewPosition(p);
    }
    
    jTextField1.setText(String.valueOf(pdfDisplay.getSelectedPage()));
    jLabel2.setText("of " + pdfDisplay.getTotalPages());
}
 
Example 16
Source File: AutoscrollSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Performs autoscroll operation.
*/
public void autoscroll(Point cursorLoc) {
    JViewport viewport = getViewport();

    if (viewport == null) {
        return;
    }

    Point viewPos = viewport.getViewPosition();
    int viewHeight = viewport.getExtentSize().height;
    int viewWidth = viewport.getExtentSize().width;

    // perform scrolling
    if ((cursorLoc.y - viewPos.y) < insets.top) {
        // scroll up
        viewport.setViewPosition(new Point(viewPos.x, Math.max(viewPos.y - scrollUnits.top, 0)));
    } else if (((viewPos.y + viewHeight) - cursorLoc.y) < insets.bottom) {
        // scroll down
        viewport.setViewPosition(
            new Point(viewPos.x, Math.min(viewPos.y + scrollUnits.bottom, comp.getHeight() - viewHeight))
        );
    } else if ((cursorLoc.x - viewPos.x) < insets.left) {
        // scroll left
        viewport.setViewPosition(new Point(Math.max(viewPos.x - scrollUnits.left, 0), viewPos.y));
    } else if (((viewPos.x + viewWidth) - cursorLoc.x) < insets.right) {
        // scroll right
        viewport.setViewPosition(
            new Point(Math.min(viewPos.x + scrollUnits.right, comp.getWidth() - viewWidth), viewPos.y)
        );
    }
}
 
Example 17
Source File: JRViewer.java    From nordpos with GNU General Public License v3.0 5 votes vote down vote up
void pnlLinksMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlLinksMouseDragged
	// Add your handling code here:

	Container container = pnlInScroll.getParent();
	if (container instanceof JViewport)
	{
		JViewport viewport = (JViewport) container;
		Point point = viewport.getViewPosition();
		int newX = point.x - (evt.getX() - downX);
		int newY = point.y - (evt.getY() - downY);

		int maxX = pnlInScroll.getWidth() - viewport.getWidth();
		int maxY = pnlInScroll.getHeight() - viewport.getHeight();

		if (newX < 0)
		{
			newX = 0;
		}
		if (newX > maxX)
		{
			newX = maxX;
		}
		if (newY < 0)
		{
			newY = 0;
		}
		if (newY > maxY)
		{
			newY = maxY;
		}

		viewport.setViewPosition(new Point(newX, newY));
	}
}
 
Example 18
Source File: BoardPanel.java    From Freerouting with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Sets the position of the viewport
 */
void set_viewport_position(java.awt.Point p_position)
{
    JViewport viewport =  scroll_pane.getViewport();
    viewport.setViewPosition(p_position);
}
 
Example 19
Source File: HeaderlessColumnResizer.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
  int mouseX = e.getX();

  TableColumn resizingColumn = table.getTableHeader().getResizingColumn();

  boolean headerLeftToRight =
    table.getTableHeader().getComponentOrientation().isLeftToRight();

  if (resizingColumn != null) {
    int oldWidth = resizingColumn.getWidth();
    int newWidth;
    if (headerLeftToRight) {
      newWidth = mouseX - mouseXOffset;
    } else {
      newWidth = mouseXOffset - mouseX;
    }
    resizingColumn.setWidth(newWidth);

    Container container;
    if ((table.getTableHeader().getParent() == null)
      || ((container = table.getTableHeader().getParent().getParent()) == null)
      || !(container instanceof JScrollPane)) {
      return;
    }

    if (!container.getComponentOrientation().isLeftToRight()
      && !headerLeftToRight) {
      if (table != null) {
        JViewport viewport = ((JScrollPane)container).getViewport();
        int viewportWidth = viewport.getWidth();
        int diff = newWidth - oldWidth;
        int newHeaderWidth = table.getWidth() + diff;

        /* Resize a table */
        Dimension tableSize = table.getSize();
        tableSize.width += diff;
        table.setSize(tableSize);

        /*
         * If this table is in AUTO_RESIZE_OFF mode and has a horizontal
         * scrollbar, we need to update a view's position.
         */
        if ((newHeaderWidth >= viewportWidth)
          && (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
          Point p = viewport.getViewPosition();
          p.x =
            Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
          viewport.setViewPosition(p);

          /* Update the original X offset value. */
          mouseXOffset += diff;
        }
      }
    }
  }
}
 
Example 20
Source File: HeaderlessColumnResizer.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
  int mouseX = e.getX();

  TableColumn resizingColumn = table.getTableHeader().getResizingColumn();

  boolean headerLeftToRight =
    table.getTableHeader().getComponentOrientation().isLeftToRight();

  if (resizingColumn != null) {
    int oldWidth = resizingColumn.getWidth();
    int newWidth;
    if (headerLeftToRight) {
      newWidth = mouseX - mouseXOffset;
    } else {
      newWidth = mouseXOffset - mouseX;
    }
    resizingColumn.setWidth(newWidth);

    Container container;
    if ((table.getTableHeader().getParent() == null)
      || ((container = table.getTableHeader().getParent().getParent()) == null)
      || !(container instanceof JScrollPane)) {
      return;
    }

    if (!container.getComponentOrientation().isLeftToRight()
      && !headerLeftToRight) {
      if (table != null) {
        JViewport viewport = ((JScrollPane)container).getViewport();
        int viewportWidth = viewport.getWidth();
        int diff = newWidth - oldWidth;
        int newHeaderWidth = table.getWidth() + diff;

        /* Resize a table */
        Dimension tableSize = table.getSize();
        tableSize.width += diff;
        table.setSize(tableSize);

        /*
         * If this table is in AUTO_RESIZE_OFF mode and has a horizontal
         * scrollbar, we need to update a view's position.
         */
        if ((newHeaderWidth >= viewportWidth)
          && (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
          Point p = viewport.getViewPosition();
          p.x =
            Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
          viewport.setViewPosition(p);

          /* Update the original X offset value. */
          mouseXOffset += diff;
        }
      }
    }
  }
}