下面列出了javax.swing.text.JTextComponent#moveCaretPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** 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);
}
}
}
/** 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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
/**
* @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);
}
}
/**
* @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);
}
}
@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;
}
}
private void setText (JTextComponent comp, String text) {
comp.setText(text);
comp.setCaretPosition(comp.getText().length());
comp.moveCaretPosition(0);
}