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

The following examples show how to use javax.swing.JViewport#getWidth() . 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: EditorUI.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Get position of the component extent. The (x, y) are set to (0, 0) if there's
* no viewport or (-x, -y) if there's one.
*/
public Rectangle getExtentBounds(Rectangle r) {
    if (r == null) {
        r = new Rectangle();
    }
    if (component != null) {
        JViewport port = getParentViewport();
        if (port != null) {
            Point p = port.getViewPosition();
            r.width = port.getWidth();
            r.height = port.getHeight();
            r.x = p.x;
            r.y = p.y;
        } else { // no viewport
            r.setBounds(component.getVisibleRect());
        }
    }
    return r;
}
 
Example 3
Source File: GraphPanel.java    From chipster with MIT License 6 votes vote down vote up
/**
 * Zooms out.
 */
private void zoomOutToolUsed() {
	if (this.setGraphScale(graph.getScale() / this.ZOOM_FACTOR)) {
		JViewport view = this.getScroller().getViewport();
		Point viewPos = view.getViewPosition();
		center = new Point((int) (viewPos.getX() + view.getWidth() / 2), (int) (viewPos.getY() + view.getHeight() / 2));

		center.x = (int) (center.getX() / this.ZOOM_FACTOR);
		center.y = (int) (center.getY() / this.ZOOM_FACTOR);

		updateGridSize();

		this.pointToCenter(center);
	}

	this.repaint();
	this.getScroller().repaint();
	graph.repaint();
}
 
Example 4
Source File: CustomTreeUI.java    From JPPF with Apache License 2.0 5 votes vote down vote up
@Override
protected AbstractLayoutCache.NodeDimensions createNodeDimensions() {
  return new NodeDimensionsHandler() {
    @Override
    public Rectangle getNodeDimensions(final Object value, final int row, final int depth, final boolean expanded, final Rectangle size) {
      final JViewport port = (JViewport) treeTable.getParent();
      final Rectangle dimensions = super.getNodeDimensions(value, row, depth, expanded, size);
      if (port != null) dimensions.width = port.getWidth();
      return dimensions;
    }
  };
}
 
Example 5
Source File: JRViewerPanel.java    From jasperreports with GNU Lesser 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 6
Source File: TileLocationBrowserUI.java    From pumpernickel with MIT License 5 votes vote down vote up
private void updateRowCount() {
	// TODO: adjust the visible row(s)?
	// that is: emphasize the selection, or else cells that were
	// previously visible before changing the row count.
	int width = list.getWidth();
	if (list.getParent() instanceof JViewport) {
		JViewport p = (JViewport) list.getParent();
		width = p.getWidth();
	}
	list.updateVisibleRowCount(width);
}
 
Example 7
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 8
Source File: FlatScrollPaneUI.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
private void mouseWheelMovedSmooth( MouseWheelEvent e ) {
		// return if there is no viewport
		JViewport viewport = scrollpane.getViewport();
		if( viewport == null )
			return;

		// find scrollbar to scroll
		JScrollBar scrollbar = scrollpane.getVerticalScrollBar();
		if( scrollbar == null || !scrollbar.isVisible() || e.isShiftDown() ) {
			scrollbar = scrollpane.getHorizontalScrollBar();
			if( scrollbar == null || !scrollbar.isVisible() )
				return;
		}

		// consume event
		e.consume();

		// get precise wheel rotation
		double rotation = e.getPreciseWheelRotation();

		// get unit and block increment
		int unitIncrement;
		int blockIncrement;
		int orientation = scrollbar.getOrientation();
		Component view = viewport.getView();
		if( view instanceof Scrollable ) {
			Scrollable scrollable = (Scrollable) view;

			// Use (0, 0) view position to obtain constant unit increment of first item
			// (which might otherwise be variable on smaller-than-unit scrolling).
			Rectangle visibleRect = new Rectangle( viewport.getViewSize() );
			unitIncrement = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
			blockIncrement = scrollable.getScrollableBlockIncrement( visibleRect, orientation, 1 );

			if( unitIncrement > 0 ) {
				// For the case that the first item (e.g. in a list) is larger
				// than the other items, get the unit increment of the second item
				// and use the smaller one.
				if( orientation == SwingConstants.VERTICAL ) {
					visibleRect.y += unitIncrement;
					visibleRect.height -= unitIncrement;
				} else {
					visibleRect.x += unitIncrement;
					visibleRect.width -= unitIncrement;
				}
				int unitIncrement2 = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
				if( unitIncrement2 > 0 )
					unitIncrement = Math.min( unitIncrement, unitIncrement2 );
			}
		} else {
			int direction = rotation < 0 ? -1 : 1;
			unitIncrement = scrollbar.getUnitIncrement( direction );
			blockIncrement = scrollbar.getBlockIncrement( direction );
		}

		// limit scroll amount (number of units to scroll) for small viewports
		// (e.g. vertical scrolling in file chooser)
		int scrollAmount = e.getScrollAmount();
		int viewportWH = (orientation == SwingConstants.VERTICAL)
			? viewport.getHeight()
			: viewport.getWidth();
		if( unitIncrement * scrollAmount > viewportWH )
			scrollAmount = Math.max( viewportWH / unitIncrement, 1 );

		// compute relative delta
		double delta = rotation * scrollAmount * unitIncrement;
		boolean adjustDelta = Math.abs( rotation ) < (1.0 + EPSILON);
		double adjustedDelta = adjustDelta
			? Math.max( -blockIncrement, Math.min( delta, blockIncrement ) )
			: delta;

		// compute new value
		int value = scrollbar.getValue();
		double minDelta = scrollbar.getMinimum() - value;
		double maxDelta = scrollbar.getMaximum() - scrollbar.getModel().getExtent() - value;
		double boundedDelta = Math.max( minDelta, Math.min( adjustedDelta, maxDelta ) );
		int newValue = value + (int) Math.round( boundedDelta );

		// set new value
		if( newValue != value )
			scrollbar.setValue( newValue );

/*debug
		System.out.println( String.format( "%4d  %9f / %4d %4d / %12f %5s %12f / %4d %4d %4d / %12f %12f %12f / %4d",
			e.getWheelRotation(),
			e.getPreciseWheelRotation(),
			unitIncrement,
			blockIncrement,
			delta,
			adjustDelta,
			adjustedDelta,
			value,
			scrollbar.getMinimum(),
			scrollbar.getMaximum(),
			minDelta,
			maxDelta,
			boundedDelta,
			newValue ) );
*/
	}
 
Example 9
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 10
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;
        }
      }
    }
  }
}
 
Example 11
Source File: GraphPanel.java    From chipster with MIT License 4 votes vote down vote up
/**
 * Zoom in
 * 
 * @param e
 */
private void zoomInToolUsed() {
	// If the zoom-limit has been reached, don't do anything
	if (this.setGraphScale(graph.getScale() * this.ZOOM_FACTOR)) {

		JViewport view = this.getScroller().getViewport();
		Point viewPos = view.getViewPosition();
		center = new Point((int) (viewPos.getX() + view.getWidth() / 2), (int) (viewPos.getY() + view.getHeight() / 2));

		logger.debug("view.getX: " + view.getX() + ", viewWidth: " + view.getWidth());

		// Make the coordinates to correspond the new size of the graph
		center.x = (int) (center.getX() * this.ZOOM_FACTOR);
		center.y = (int) (center.getY() * this.ZOOM_FACTOR);

		/*
		 * The new scale won't be applied before this method is ended. The new scale changes the size of the graph, which affects on the
		 * calculations done in method pointToCenter. We couuld estimate the new size of the graph, but that doesn't help because we
		 * still cant scroll to the new areas.
		 */

		Runnable centerer = new Thread() {
			public void run() {
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					// Nothing serious happened
				}
				GraphPanel.this.pointToCenter(GraphPanel.this.center);
			}
		};

		updateGridSize();

		// try it to avoid flickering when possible
		this.pointToCenter(GraphPanel.this.center);

		// and make sure it's done in every situation;
		SwingUtilities.invokeLater(centerer);
	}
	graph.repaint();
}