javax.swing.text.Caret类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中javax.swing.text.Caret类的一些代码示例,展示了Caret类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Caret类的具体详情如下:
包路径:javax.swing.text.Caret
类名称:Caret

Caret介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

getDocument().addDocumentListener( new DocumentListener() {
 @Override
 public void insertUpdate( DocumentEvent e ) {
if ( formatter != null ) {
 try {
  formatter.stringToValue( getText() );
  return true;
 } catch ( ParseException e ) {
 int old_caret_position = getCaretPosition();
 super.setValue( value );
 setCaretPosition( Math.min( old_caret_position, getText().length() ) );
 final int dot = field.getCaret().getDot();
 final int mark = field.getCaret().getMark();
 if ( field.isEnabled() && field.isEditable() ) {
  SwingUtilities.invokeLater( new Runnable() {
     field.getCaret().setDot( dot );

代码示例来源:origin: ron190/jsql-injection

@Override
  public void focusGained(FocusEvent arg0) {
    DialogTranslate.this.textToTranslate[0].getCaret().setVisible(true);
    DialogTranslate.this.textToTranslate[0].getCaret().setSelectionVisible(true);
  }
});

代码示例来源:origin: bobbylight/RSyntaxTextArea

/**
 * Returns the default caret for an <code>RTextArea</code>.  This caret is
 * capable of displaying itself differently for insert/overwrite modes.
 *
 * @return The caret.
 */
@Override
protected Caret createCaret() {
  Caret caret = new ConfigurableCaret();
  caret.setBlinkRate(500);
  return caret;
}

代码示例来源:origin: JetBrains/ideavim

Document doc = target.getDocument();
Caret caret = target.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
 doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
  String dotChars = doc.getText(dot - 2, 2);
  char c0 = dotChars.charAt(0);
  char c1 = dotChars.charAt(1);
 doc.remove(dot - delChars, delChars);

代码示例来源:origin: groovy/groovy-core

overtypeCaret.setBlinkRate(defaultCaret.getBlinkRate());

代码示例来源:origin: ron190/jsql-injection

JTextComponent target = this.getTextComponent(e);
if ((target != null) && (target.isEditable())) {
  try {
    Document doc = target.getDocument();
    Caret caret = target.getCaret();
    int dot = caret.getDot();
    int mark = caret.getMark();
    if (dot != mark) {
      doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
    } else if (dot < doc.getLength()) {
      int delChars = 1;
      if (dot < doc.getLength() - 1) {
        String dotChars = doc.getText(dot, 2);
        char c0 = dotChars.charAt(0);

代码示例来源:origin: net.java.abeille/abeille

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      if (!target.isEditable() || !target.isEnabled()) {
        target.getToolkit().beep();
        return;
      }
      Caret caret = target.getCaret();
      try {
        BaseDocument doc = (BaseDocument) target.getDocument();
        int dotPos = caret.getDot();
        int bolPos = Utilities.getRowStart(doc, dotPos);
        int wsPos = Utilities.getPreviousWord(target, dotPos);
        wsPos = (dotPos == bolPos) ? wsPos : Math.max(bolPos, wsPos);
        doc.remove(wsPos, dotPos - wsPos);
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      }
    }
  }
}

代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      Caret caret = target.getCaret();
      int dot = target.getDocument().getLength(); // end of document
      if (select) {
        caret.moveDot(dot);
      } else {
        caret.setDot(dot);
      }
    }
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-jsf

public static boolean isInViewTag(JsfLibrariesSupport jls, JTextComponent targetComponent) {
  try {
    Caret caret = targetComponent.getCaret();
    int position0 = Math.min(caret.getDot(), caret.getMark());
    int position1 = Math.max(caret.getDot(), caret.getMark());
    int len = targetComponent.getDocument().getLength() - position1;
    return targetComponent.getText(0, position0).contains(PaletteUtils.createViewTag(jls, targetComponent, false))
        && targetComponent.getText(position1, len).contains(PaletteUtils.createViewTag(jls, targetComponent, true));
  } catch (BadLocationException ble) {
    Exceptions.printStackTrace(ble);
    // we don't know; let's assume we are:
    return true;
  }
}

代码示例来源:origin: ron190/jsql-injection

@Override
public void actionPerformed(ActionEvent e) {
  JTextComponent target = this.getTextComponent(e);
  if (Objects.nonNull(target) && target.isEditable()) {
    Caret caret = target.getCaret();
    int dot  = caret.getDot();
    int mark = caret.getMark();
    if (DefaultEditorKit.deletePrevCharAction.equals(this.getValue(Action.NAME))) {
      // @see javax/swing/text/DefaultEditorKit.java DeletePrevCharAction
      if (dot == 0 && mark == 0) {
        return;
      }
    } else {
      // @see javax/swing/text/DefaultEditorKit.java DeleteNextCharAction
      Document doc = target.getDocument();
      if (dot == mark && doc.getLength() == dot) {
        return;
      }
    }
  }
  this.deleteAction.actionPerformed(e);
}

代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib

public void actionPerformed(ActionEvent evt, JTextComponent target) {
  if (target != null) {
    if (!target.isEditable() || !target.isEnabled()) {
      target.getToolkit().beep();
      return;
    EditorUI editorUI = Utilities.getEditorUI(target);
    Caret caret = target.getCaret();
    final BaseDocument doc = Utilities.getDocument(target);
    if (caret.isSelectionVisible()) {
      target.replaceSelection(null);
    int dotPos = caret.getDot();
    String s = editorUI.getWordMatch().getMatchWord(dotPos, direction);
    String prevWord = editorUI.getWordMatch().getPreviousWord();
    if (s != null) {
      doc.atomicLock();
      try {
        int pos = dotPos;
        if (prevWord != null && prevWord.length() > 0) {
          pos -= prevWord.length();
          doc.remove(pos, prevWord.length());
        doc.insertString(pos, s, null);
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      } finally {
        doc.atomicUnlock();

代码示例来源:origin: JetBrains/ideavim

/**
  * Invoked when an action occurs.
  */
 public void actionPerformed(ActionEvent e) {
  ExTextField target = (ExTextField)getTextComponent(e);
  target.saveLastEntry();
  Document doc = target.getDocument();
  Caret caret = target.getCaret();
  try {
   doc.remove(caret.getDot(), doc.getLength());
  }
  catch (BadLocationException ex) {
   // ignore
  }
 }
}

代码示例来源:origin: net.java.abeille/abeille

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      Caret caret = target.getCaret();
      try {
        BaseDocument doc = (BaseDocument) target.getDocument();
        doc.toggleBookmark(caret.getDot());
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      }
    }
  }
}

代码示例来源:origin: net.java.abeille/abeille

public void actionPerformed(ActionEvent evt, JTextComponent target) {
  if (target != null) {
    if (!target.isEditable() || !target.isEnabled()) {
      target.getToolkit().beep();
      return;
    }
    target.replaceSelection("");
    Caret caret = target.getCaret();
    int dotPos = caret.getDot();
    BaseDocument doc = (BaseDocument) target.getDocument();
    dotPos = doc.getFormatter().indentNewLine(doc, dotPos);
    caret.setDot(dotPos);
  }
}

代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      if (!target.isEditable() || !target.isEnabled()) {
        target.getToolkit().beep();
        return;
      }
      Caret caret = target.getCaret();
      try {
        BaseDocument doc = (BaseDocument)target.getDocument();
        int dotPos = caret.getDot();
        int bolPos = Utilities.getRowStart(target, dotPos);
        int eolPos = Utilities.getRowEnd(target, dotPos);
        eolPos = Math.min(eolPos + 1, doc.getLength()); // include '\n'
        doc.remove(bolPos, eolPos - bolPos);
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      }
    }
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-jsf

private static int insert(String s, JTextComponent target, Document doc) throws BadLocationException {
  int start = -1;
  try {
    //at first, find selected text range
    Caret caret = target.getCaret();
    int p0 = Math.min(caret.getDot(), caret.getMark());
    int p1 = Math.max(caret.getDot(), caret.getMark());
    doc.remove(p0, p1 - p0);
    //replace selected text by the inserted one
    start = caret.getDot();
    doc.insertString(start, s, null);
  } catch (BadLocationException ble) {
  }
  return start;
}

代码示例来源:origin: net.java.abeille/abeille

public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
      Caret caret = target.getCaret();
      try {
        int pos = Utilities.getRowFirstNonWhite((BaseDocument) target.getDocument(), caret.getDot());
        if (pos >= 0) {
          if (select) {
            caret.moveDot(pos);
          }
          else {
            caret.setDot(pos);
          }
        }
      } catch (BadLocationException e) {
        target.getToolkit().beep();
      }
    }
  }
}

代码示例来源:origin: net.java.abeille/abeille

public boolean gotoDeclaration(JTextComponent target) {
  try {
    Caret caret = target.getCaret();
    int dotPos = caret.getDot();
    BaseDocument doc = (BaseDocument) target.getDocument();
    int[] idBlk = Utilities.getIdentifierBlock(doc, dotPos);
    ExtSyntaxSupport extSup = (ExtSyntaxSupport) doc.getSyntaxSupport();
    if (idBlk != null) {
      int decPos = extSup.findDeclarationPosition(doc.getText(idBlk), idBlk[1]);
      if (decPos >= 0) {
        caret.setDot(decPos);
        return true;
      }
    }
  } catch (BadLocationException e) {
  }
  return false;
}

代码示例来源:origin: bobbylight/RSyntaxTextArea

Color selBG = textArea.getSelectionColor();
  Color selFG = textArea.getSelectedTextColor();
  textArea.setUI(new javax.swing.plaf.basic.BasicEditorPaneUI());
  textArea.setSelectedTextColor(selFG);
  textArea.setSelectionColor(selBG);
textArea.getCaret().setSelectionVisible(true);

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-mobility-editor

protected boolean canFilter(final JTextComponent component) {
  filterPrefix = null;
  try {
    final int ancor = Utilities.getPreviousWord(component, component.getCaret().getDot());
    filterPrefix = component.getDocument().getText(ancor, component.getCaret().getDot() - ancor);
    if (PREFIX.equals(filterPrefix))
      filterPrefix = "";
    else if (!canBeValidDirective(filterPrefix) || "".equals(filterPrefix))
      filterPrefix = null;
  } catch (BadLocationException e) {
    // filterPrefix stays null -> no filtering
  }
  return (filterPrefix != null);
}

相关文章