javax.swing.JTextArea.getSelectionStart()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(185)

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

JTextArea.getSelectionStart介绍

暂无

代码示例

代码示例来源:origin: net.java.openjdk.cacio/cacio-shared

@Override
public int getSelectionStart() {
  return getTextArea().getSelectionStart();
}

代码示例来源:origin: mucommander/mucommander

void findPrevious() {
  doSearch(textArea.getSelectionStart() - 1, false);
}

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

final JTextArea textArea = new TextClass();
...
textArea.addKeyListener(new KeyListener() {
  @Override
  public void keyTyped(final KeyEvent e) {
    char key = e.getKeyChar();
    switch (key) {
      case '<':
        System.out.print("   " + textArea.getSelectionStart());
    }
  }

  @Override
  public void keyPressed(final KeyEvent e) {
  }

  @Override
  public void keyReleased(final KeyEvent e) {
  }
});

代码示例来源:origin: orbisgis/orbisgis

/**
 * Return true iff the selection is already block commented.
 * @param scriptPanel Script panel
 * @return True iff the selection is already block commented
 */
private static boolean alreadyBlockCommented(JTextArea scriptPanel) {
  try {
    return scriptPanel.getText(scriptPanel.getSelectionStart(), BLOCK_COMMENT_START.length()).equals(BLOCK_COMMENT_START)
        && scriptPanel.getText(scriptPanel.getSelectionEnd() - BLOCK_COMMENT_END.length(), BLOCK_COMMENT_END.length()).equals(BLOCK_COMMENT_END);
  } catch (BadLocationException e) {
    LOGGER.warn(I18N.tr("Assuming the selection is not already block commented."), e);
  }
  return false;
}

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

int selStart = textArea.getSelectionStart();

代码示例来源:origin: GoldenGnu/jeveassets

private void fromClipboard() {
  Toolkit tk = Toolkit.getDefaultToolkit();
  Clipboard clipboard = tk.getSystemClipboard();
  Transferable transferable = clipboard.getContents(this);
  try {
    String s = (String) transferable.getTransferData(DataFlavor.stringFlavor);
    String text = jText.getText();
    String before = text.substring(0, jText.getSelectionStart());
    String after = text.substring(jText.getSelectionEnd(), text.length());
    jText.setText(before + s + after);
    int caretPosition = before.length() + s.length();
    if (caretPosition <= jText.getText().length()) {
      jText.setCaretPosition(before.length() + s.length());
    }
  } catch (UnsupportedFlavorException ex) {
  } catch (IOException ex) {
  }
}

代码示例来源:origin: IanDarwin/javasrc

/** Format the selection (like unix fmt) */
void format() {
  // "The 50-line text formatter" in 15 lines.
  int col = 0;
  int start = tf.getSelectionStart();
  int end   = tf.getSelectionEnd();
  final String selectedText = tf.getSelectedText();
  if (selectedText == null) {
    JOptionPane.showMessageDialog(this, "Please select text before formatting");
    return;
  }
  StringTokenizer st = new StringTokenizer(selectedText);
  StringBuilder s = new StringBuilder();
  while (st.hasMoreTokens()) {
    String sval = st.nextToken();
    if (col+sval.length() > COLS-7) {
      s.append("\n");
      col = 0;
    }
    s.append(sval).append(' ');
    col+=sval.length()+1;
  }
  tf.replaceRange(s.toString(), start, end);
}

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

JTextArea textArea = new JTextArea("some text");
int start = textArea.getSelectionStart();
int end = textArea.getSelectionEnd();
String replace = textArea.getSelectedText().toUpperCase();
textArea.replaceRange(replace, start, end);

代码示例来源:origin: orbisgis/orbisgis

/**
 * Block uncomment the selected text in the given script panel.
 *
 * @param scriptPanel Script panel
 */
private static void blockUncomment(JTextArea scriptPanel) {
  // Recover the index of the start of the selection.
  final int startOffset = scriptPanel.getSelectionStart();
  final int endOffset = scriptPanel.getSelectionEnd();
  // Delete the comment characters.
  scriptPanel.replaceRange("", endOffset - BLOCK_COMMENT_END.length(), endOffset);
  scriptPanel.replaceRange("", startOffset, startOffset + BLOCK_COMMENT_START.length());
  // Select the uncommented selection.
  scriptPanel.setSelectionStart(startOffset);
  scriptPanel.setSelectionEnd(endOffset - BLOCK_COMMENT_START.length()
      - BLOCK_COMMENT_END.length());
}

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

@Override
public void caretUpdate(CaretEvent e) {
  int length = ta.getSelectionEnd() - ta.getSelectionStart();
  System.out.println(length);

代码示例来源:origin: orbisgis/orbisgis

/**
 * Block comment the selected text in the given script panel.
 *
 * @param scriptPanel Script panel
 */
private static void blockComment(JTextArea scriptPanel) {
  // Recover the index of the start of the selection.
  final int startOffset = scriptPanel.getSelectionStart();
  // Comment the selection.
  final String commentedSelection = BLOCK_COMMENT_START
      + scriptPanel.getSelectedText() + BLOCK_COMMENT_END;
  scriptPanel.replaceSelection(commentedSelection);
  // Select the commented selection.
  scriptPanel.setSelectionStart(startOffset);
  scriptPanel.setSelectionEnd(startOffset + commentedSelection.length());
}

代码示例来源:origin: eu.mihosoft.vrl/vrl

@Override
  public void actionPerformed(ActionEvent e) {
    JTextArea editor = tRep.getEditor().getEditor();
    Document document = editor.getDocument();
    int selectionStart = editor.getSelectionStart();
    int selectionEnd = editor.getSelectionEnd();
    try {
      int firstLineOffset = editor.getLineOfOffset(selectionStart);
      int lastLineOffset = editor.getLineOfOffset(selectionEnd);
      for (int l = firstLineOffset; l <= lastLineOffset; l++) {
        int offset = editor.getLineStartOffset(l);
        document.insertString(offset, "//", null);
      }
    } catch (BadLocationException ex) {
      Logger.getLogger(GroovyCodeEditorComponent.class.getName()).
          log(Level.SEVERE, null, ex);
    }
  }
}, comment, JComponent.WHEN_FOCUSED);

代码示例来源:origin: orbisgis/orbisgis

/**
   * Uncomment the selected text in the given script panel.
   *
   * @param scriptPanel Script panel
   */
  private static void uncommentSQL(JTextArea scriptPanel, String commentCharacter) {

    final Element root = scriptPanel.getDocument().getDefaultRootElement();

    final int numberOfLastLine = root.getElementIndex(scriptPanel.getSelectionEnd());
    int currentLineNumber = root.getElementIndex(scriptPanel.getSelectionStart());
    while (currentLineNumber <= numberOfLastLine) {
      try {
        int startOffset = root.getElement(currentLineNumber).getStartOffset();
        if (scriptPanel.getText(startOffset, commentCharacter.length()).equals(commentCharacter)) {
          scriptPanel.replaceRange("", startOffset, startOffset + commentCharacter.length());
        }
      } catch (BadLocationException e) {
        LOGGER.warn(I18N.tr("Invalid length or offset when trying to uncomment code."), e);
      }
      currentLineNumber++;
    }
  }
}

代码示例来源:origin: UNIVALI-LITE/Portugol-Studio

private boolean importaStringArrastada(TransferHandler.TransferSupport support) 
{
  try 
  {
    String stringArrastada = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
    if (stringArrastada.equals(textArea.getSelectedText())) {
      if (stringArrastada.isEmpty() || programa == null) {
        return false;
      }
      int linha = textArea.getLineOfOffset(textArea.getSelectionStart()) + 1;
      int coluna = textArea.getSelectionStart() - textArea.getLineStartOffset(linha - 1);
      int tamanhoDoTexto = textArea.getSelectionEnd() - textArea.getSelectionStart();
      ProcuradorDeDeclaracao procuradorDeDeclaracao = new ProcuradorDeDeclaracao(stringArrastada, linha, coluna, tamanhoDoTexto);
      programa.getArvoreSintaticaAbstrata().aceitar(procuradorDeDeclaracao);
      NoDeclaracao no = procuradorDeDeclaracao.getNoDeclaracao();
      if (procuradorDeDeclaracao.encontrou() && !contemNo(no)) {
        adicionaNo(no);
      }
    }
  } catch (Exception e) {
    return false;
  }
  
  return false;
}

代码示例来源:origin: orbisgis/orbisgis

/**
 * Test whether the selected text consists of an unbroken range of
 * commented lines.
 *
 * @param scriptPanel Script panel
 * @return True iff the selected text consists of an unbroken range of
 * commented lines
 */
private static boolean unbrokenRangeOfComments(JTextArea scriptPanel, String commentCharacter) {
  final Element root = scriptPanel.getDocument().getDefaultRootElement();
  final int numberOfLastLine = root.getElementIndex(scriptPanel.getSelectionEnd());
  int currentLineNumber = root.getElementIndex(scriptPanel.getSelectionStart());
  while (currentLineNumber <= numberOfLastLine) {
    try {
      int startOffset = root.getElement(currentLineNumber).getStartOffset();
      if (!scriptPanel.getText(startOffset, commentCharacter.length()).equals(commentCharacter)) {
        return false;
      }
    } catch (BadLocationException e) {
      LOGGER.warn(I18N.tr("Problem when checking for an unbroken range of comments"), e);
    }
    currentLineNumber++;
  }
  return true;
}

代码示例来源:origin: eu.mihosoft.vrl/vrl

int caretPosition = editor.getCaretPosition();
Document document = editor.getDocument();
int selectionStart = editor.getSelectionStart();
int selectionEnd = editor.getSelectionEnd();

代码示例来源:origin: senbox-org/snap-desktop

int selPos1 = codeArea.getSelectionStart();
int selPos2 = codeArea.getSelectionEnd();
if (selPos1 >= 0 && selPos2 >= 0 && selPos1 > selPos2) {

代码示例来源:origin: com.google.code.findbugs/findbugs

protected void commentBoxClicked() {
  if (commentWasChanged()) {
    return;
  }
  setCanAddComments(false, true);
  CommentInfo commentInfo = new CommentInfo().invoke();
  boolean sameText = commentInfo.isSameText();
  String txt = commentInfo.getTxt();
  if (!sameText) {
    txt = "";
  }
  if (txt == null || txt.trim().length() == 0) {
    txt = "";
  }
  resetCommentBoxFont();
  boolean sameTextInBox = commentBox.getText().equals(txt);
  setCommentText(txt);
  int start = commentBox.getSelectionStart();
  int end = commentBox.getSelectionEnd();
  if (!commentBox.hasFocus() && (!sameTextInBox || start != 0 || end != txt.length())) {
    commentBox.setSelectionStart(0);
    commentBox.setSelectionEnd(txt.length());
  }
  updateSaveButton();
}

代码示例来源:origin: orbisgis/orbisgis

/**
 * Comment the selected text in the given script panel.
 *
 * @param scriptPanel Script panel
 */
private static void commentSQL(JTextArea scriptPanel, String commentCharacter) {
  final Element root = scriptPanel.getDocument().getDefaultRootElement();
  final int numberOfLastLine = root.getElementIndex(scriptPanel.getSelectionEnd());
  int currentLineNumber = root.getElementIndex(scriptPanel.getSelectionStart());
  while (currentLineNumber <= numberOfLastLine) {
    scriptPanel.insert(commentCharacter,
        root.getElement(currentLineNumber).getStartOffset());
    currentLineNumber++;
  }
}

代码示例来源:origin: org.apache.uima/uimaj-tools

final int currentSelStart = this.main.getTextArea().getSelectionStart();
if (node.isAnnotation()) {
 if (null != this.main.getCas().getDocumentText()) {

相关文章

JTextArea类方法