Java Code Examples for javax.swing.text.JTextComponent#scrollRectToVisible()

The following examples show how to use javax.swing.text.JTextComponent#scrollRectToVisible() . 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: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 2
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 3
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 4
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 5
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 6
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 7
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 8
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 9
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
	Highlighter highlighter = comp.getHighlighter();
	highlighter.removeAllHighlights();

	try {
		i = toComponentPosition(comp, i);
		j = toComponentPosition(comp, j);
		highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
		if (scroll) {
			if (comp.getCaretPosition() < i || comp.getCaretPosition() > j) {
				comp.moveCaretPosition(i);
				comp.scrollRectToVisible(comp.modelToView(i));
			}
		}
	}
	catch (BadLocationException ble) {
		errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
	}
}
 
Example 10
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 11
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 12
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 13
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static void scrollToCenter(JTextComponent tc, int pos) throws BadLocationException {
  Rectangle rect = tc.modelToView(pos);
  // Java 9: Rectangle rect = tc.modelToView2D(pos).getBounds();
  Container c = SwingUtilities.getAncestorOfClass(JViewport.class, tc);
  if (Objects.nonNull(rect) && c instanceof JViewport) {
    rect.x = Math.round(rect.x - c.getWidth() / 2f);
    rect.width = c.getWidth();
    rect.height = Math.round(c.getHeight() / 2f);
    tc.scrollRectToVisible(rect);
  }
}
 
Example 14
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static void scrollToCenter(JTextComponent tc, int pos) throws BadLocationException {
  Rectangle rect = tc.modelToView(pos);
  // Java 9: Rectangle rect = tc.modelToView2D(pos).getBounds();
  Container c = SwingUtilities.getAncestorOfClass(JViewport.class, tc);
  if (Objects.nonNull(rect) && c instanceof JViewport) {
    rect.x = (int) (rect.x - c.getWidth() * .5);
    rect.width = c.getWidth();
    rect.height = (int) (c.getHeight() * .5);
    tc.scrollRectToVisible(rect);
  }
}
 
Example 15
Source File: QuickFindDialog.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
public void showFor(final JTextComponent target) {
    oldCaretPosition = target.getCaretPosition();
    Container view = target.getParent();
    Dimension wd = getPreferredSize();
    //wd.width = target.getVisibleRect().width;
    Point loc = new Point(0, view.getHeight());
    setSize(wd);
    setLocationRelativeTo(view);
    SwingUtilities.convertPointToScreen(loc, view);
    setLocation(loc);
    jTxtFind.setFont(target.getFont());
    jTxtFind.getDocument().addDocumentListener(this);
    WindowAdapter closeListener = new WindowAdapter() {

        @Override
        public void windowDeactivated(WindowEvent e) {
            target.getDocument().removeDocumentListener(QuickFindDialog.this);
            Markers.removeMarkers(target, marker);
            if (escaped) {
                Rectangle aRect;
                try {
                    aRect = target.modelToView(oldCaretPosition);
                    target.setCaretPosition(oldCaretPosition);
                    target.scrollRectToVisible(aRect);
                } catch (BadLocationException ex) {
                }
            }
            dispose();
        }
    };
    addWindowListener(closeListener);
    this.target = new WeakReference<JTextComponent>(target);
    Pattern p = dsd.get().getPattern();
    if (p != null) {
        jTxtFind.setText(p.pattern());
    }
    jChkWrap.setSelected(dsd.get().isWrap());
    setVisible(true);
    jTxtFind.selectAll();
}
 
Example 16
Source File: Utility.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Scrolls the specified text component so the text between the starting and ending index are visible.
 */
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
    try {
        Rectangle startingRectangle = textComponent.modelToView(startingIndex);
        Rectangle endDingRectangle = textComponent.modelToView(endingIndex);

        Rectangle totalBounds = startingRectangle.union(endDingRectangle);

        textComponent.scrollRectToVisible(totalBounds);
        textComponent.repaint();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 17
Source File: Utility.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Scrolls the specified text component so the text between the starting and ending index are visible.
 */
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
    try {
        Rectangle startingRectangle = textComponent.modelToView(startingIndex);
        Rectangle endDingRectangle = textComponent.modelToView(endingIndex);

        Rectangle totalBounds = startingRectangle.union(endDingRectangle);

        textComponent.scrollRectToVisible(totalBounds);
        textComponent.repaint();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 18
Source File: EditorFindSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure that the given region will be visible in the view
 * with the appropriate find insets.
 */
private void ensureVisible(JTextComponent c, int startOffset, int endOffset, Insets extraInsets) {
    try {
        Rectangle startBounds = c.modelToView(startOffset);
        Rectangle endBounds = c.modelToView(endOffset);
        if (startBounds != null && endBounds != null) {
            startBounds.add(endBounds);
            if (extraInsets != null) {
                Rectangle visibleBounds = c.getVisibleRect();
                int extraTop = (extraInsets.top < 0)
                    ? -extraInsets.top * visibleBounds.height / 100 // percentage
                    : extraInsets.top * endBounds.height; // line count
                startBounds.y -= extraTop;
                startBounds.height += extraTop;
                startBounds.height += (extraInsets.bottom < 0)
                    ? -extraInsets.bottom * visibleBounds.height / 100 // percentage
                    : extraInsets.bottom * endBounds.height; // line count
                int extraLeft = (extraInsets.left < 0)
                    ? -extraInsets.left * visibleBounds.width / 100 // percentage
                    : extraInsets.left * endBounds.width; // char count
                startBounds.x -= extraLeft;
                startBounds.width += extraLeft;
                startBounds.width += (extraInsets.right < 0)
                    ? -extraInsets.right * visibleBounds.width / 100 // percentage
                    : extraInsets.right * endBounds.width; // char count
            }
            c.scrollRectToVisible(startBounds);
        }
    } catch (BadLocationException e) {
        // do not scroll
    }
}
 
Example 19
Source File: Utility.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Scrolls the specified text component so the text between the starting and ending index are visible.
 */
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
    try {
        Rectangle startingRectangle = textComponent.modelToView(startingIndex);
        Rectangle endDingRectangle = textComponent.modelToView(endingIndex);

        Rectangle totalBounds = startingRectangle.union(endDingRectangle);

        textComponent.scrollRectToVisible(totalBounds);
        textComponent.repaint();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 20
Source File: OverrideEditorActions.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        Caret caret = target.getCaret();
        BaseDocument doc = (BaseDocument)target.getDocument();
        try {
            int dot = caret.getDot();
            // #232675: if bounds are defined, use them rather than line start/end
            Object o = target.getClientProperty(PROP_NAVIGATE_BOUNDARIES);
            PositionRegion bounds = null;
            int lineStartPos = Utilities.getRowStart(target, dot);
            
            if (o instanceof PositionRegion) {
                bounds = (PositionRegion)o;
                int start = bounds.getStartOffset();
                int end = bounds.getEndOffset();
                int boundLineStart = Utilities.getRowStart(target, start);
                // refinement: only use the boundaries if the caret is at the same line
                // as boundary start; otherwise ignore the boundary and use document lines.
                if (boundLineStart == lineStartPos && dot > start && dot <= end) {
                    // move to the region start
                    dot = start;
                } else {
                    bounds = null;
                }
            }
            
            if (bounds == null) {
                if (homeKeyColumnOne) { // to first column
                    dot = lineStartPos;
                } else { // either to line start or text start
                    int textStartPos = Utilities.getRowFirstNonWhite(doc, lineStartPos);
                    if (textStartPos < 0) { // no text on the line
                        textStartPos = Utilities.getRowEnd(target, lineStartPos);
                    }
                    if (dot == lineStartPos) { // go to the text start pos
                        dot = textStartPos;
                    } else if (dot <= textStartPos) {
                        dot = lineStartPos;
                    } else {
                        dot = textStartPos;
                    }
                }
            }
            // For partial view hierarchy check bounds
            dot = Math.max(dot, target.getUI().getRootView(target).getStartOffset());
            String actionName = (String) getValue(Action.NAME);
            boolean select = selectionBeginLineAction.equals(actionName)
                    || selectionLineFirstColumnAction.equals(actionName);
            
            // If possible scroll the view to its begining horizontally
            // to ease user's orientation in the code.
            Rectangle r = target.modelToView(dot);
            Rectangle visRect = target.getVisibleRect();
            if (r.getMaxX() < visRect.getWidth()) {
                r.x = 0;
                target.scrollRectToVisible(r);
            }
            target.putClientProperty("navigational.action", SwingConstants.WEST);
            if (select) {
                caret.moveDot(dot);
            } else {
                caret.setDot(dot);
            }
        } catch (BadLocationException e) {
            target.getToolkit().beep();
        }
    }
}