javax.swing.text.JTextComponent#moveCaretPosition ( )源码实例Demo

下面列出了javax.swing.text.JTextComponent#moveCaretPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: netbeans   文件: OutputEditorKit.java
/** The operation to perform when this action is triggered. */
public void actionPerformed(ActionEvent e) {
    JTextComponent target = getTextComponent(e);
    if (target != null) {
        Document doc = target.getDocument();
        Element map = doc.getDefaultRootElement();
        int offs = target.getCaretPosition();
        int lineIndex = map.getElementIndex(offs);
        int lineStart = map.getElement(lineIndex).getStartOffset();

        if (select) {
            target.moveCaretPosition(lineStart);
        } else {
            target.setCaretPosition(lineStart);
        }
    }
}
 
源代码2 项目: netbeans   文件: OutputEditorKit.java
/** The operation to perform when this action is triggered. */
public void actionPerformed(ActionEvent e) {
    JTextComponent target = getTextComponent(e);
    if (target != null) {
        Document doc = target.getDocument();
        Element map = doc.getDefaultRootElement();
        int offs = target.getCaretPosition();
        int lineIndex = map.getElementIndex(offs);
        int lineEnd = map.getElement(lineIndex).getEndOffset() - 1;

        if (select) {
            target.moveCaretPosition(lineEnd);
        } else {
            target.setCaretPosition(lineEnd);
        }
    }            
}
 
源代码3 项目: codebuff   文件: STViz.java
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);
	}
}
 
源代码4 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码5 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码6 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码7 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码8 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码9 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码10 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码11 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码12 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码13 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码14 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码15 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码16 项目: codebuff   文件: STViz.java
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);
    }
}
 
源代码17 项目: seaglass   文件: MacEditorKit.java
/**
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
public void actionPerformed(ActionEvent e) {
    JTextComponent target = getTextComponent(e);
    boolean        beep   = true;

    if ((target != null) && (target.isEditable())) {
        int     offs   = target.getCaretPosition();
        boolean failed = false;

        try {
            offs = Utilities.getPreviousWord(target, offs);
        } catch (BadLocationException bl) {

            if (offs != 0) {
                offs = 0;
            } else {
                failed = true;
            }
        }

        if (!failed) {
            target.moveCaretPosition(offs);

            // and then delete it
            target.replaceSelection("");
            beep = false;
        }
    }

    if (beep) {
        provideErrorFeedback(target);
    }
}
 
源代码18 项目: seaglass   文件: MacEditorKit.java
/**
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
public void actionPerformed(ActionEvent e) {
    JTextComponent target = getTextComponent(e);
    boolean        beep   = true;

    if ((target != null) && (target.isEditable())) {

        try {

            // select the next word
            int    offs    = target.getCaretPosition();
            int    endOffs;
            String s       = target.getDocument().getText(offs, 1);

            if (Character.isWhitespace(s.charAt(0))) {
                endOffs = Utilities.getNextWord(target, offs);
                endOffs = Utilities.getWordEnd(target, endOffs);
            } else {
                endOffs = Utilities.getWordEnd(target, offs);
            }

            target.moveCaretPosition(endOffs);

            // and then delete it
            target.replaceSelection("");
            beep = false;
        } catch (BadLocationException exc) {
            // nothing to do, because we set beep to true already
        }
    }

    if (beep) {
        provideErrorFeedback(target);
    }
}
 
源代码19 项目: wpcleaner   文件: FindTextAction.java
@Override
public void actionPerformed(ActionEvent e) {
  JTextComponent text = (textPane != null) ? textPane : getTextComponent(e);
  String currentSearch = search;
  if ((currentSearch == null) || (currentSearch.isEmpty())) {
    currentSearch = text.getSelectedText();
  }
  if ((currentSearch == null) || (currentSearch.isEmpty())) {
    currentSearch = lastSearch;
  }
  currentSearch = JOptionPane.showInputDialog(
      text.getParent(),
      GT._T("String to find"),
      currentSearch);
  if ((currentSearch == null) || ("".equals(currentSearch.trim()))) {
    return;
  }
  lastSearch = currentSearch;
  String textPattern = "";
  char firstChar = lastSearch.charAt(0);
  if (Character.isLetter(firstChar)) {
    textPattern =
        "[" + Character.toUpperCase(firstChar) + Character.toLowerCase(firstChar) + "]" +
        Pattern.quote(lastSearch.substring(1));
  } else {
    textPattern = Pattern.quote(lastSearch);
  }
  Pattern pattern = Pattern.compile(textPattern);
  Matcher matcher = pattern.matcher(text.getText());
  if (matcher.find(text.getCaretPosition())) {
    text.setCaretPosition(matcher.start());
    text.moveCaretPosition(matcher.end());
    text.requestFocus();
    return;
  }
  if (matcher.find(0)) {
    text.setCaretPosition(matcher.start());
    text.moveCaretPosition(matcher.end());
    text.requestFocus();
    return;
  }
}
 
源代码20 项目: netbeans   文件: ManageTags.java
private void setText (JTextComponent comp, String text) {
    comp.setText(text);
    comp.setCaretPosition(comp.getText().length());
    comp.moveCaretPosition(0);
}