/** * Return the line of text at the given position. The returned value may * be null. It will not contain the trailing new-line character. * @param target the text component * @param pos char position */ public static String getLineAt(JTextComponent target, int pos) { String line = null; Document doc = target.getDocument(); if (doc instanceof PlainDocument) { PlainDocument pDoc = (PlainDocument) doc; int start = pDoc.getParagraphElement(pos).getStartOffset(); int end = pDoc.getParagraphElement(pos).getEndOffset(); try { line = doc.getText(start, end - start); if (line != null && line.endsWith("\n")) { line = line.substring(0, line.length() - 1); } } catch (BadLocationException ex) { Logger.getLogger(ActionUtils.class.getName()).log(Level.SEVERE, null, ex); } } return line; }
/** * Return the line of text at the given position. The returned value may * be null. It will not contain the trailing new-line character. * @param target the text component * @param pos char position * @return */ public static String getLineAt(JTextComponent target, int pos) { String line = null; Document doc = target.getDocument(); if (doc instanceof PlainDocument) { PlainDocument pDoc = (PlainDocument) doc; int start = pDoc.getParagraphElement(pos).getStartOffset(); int end = pDoc.getParagraphElement(pos).getEndOffset(); try { line = doc.getText(start, end - start); if (line != null && line.endsWith("\n")) { line = line.substring(0, line.length() - 1); } } catch (BadLocationException ex) { Logger.getLogger(ActionUtils.class.getName()).log(Level.SEVERE, null, ex); } } return line; }
try { PlainDocument pDoc = (PlainDocument) target.getDocument(); int start = pDoc.getParagraphElement(target.getSelectionStart()).getStartOffset(); int end; if (target.getSelectionStart() == target.getSelectionEnd()) { end = pDoc.getParagraphElement(target.getSelectionEnd()).getEndOffset(); } else { end = pDoc.getParagraphElement(target.getSelectionEnd() - 1).getEndOffset();
try { PlainDocument pDoc = (PlainDocument) target.getDocument(); int start = pDoc.getParagraphElement(target.getSelectionStart()).getStartOffset(); int end; if (target.getSelectionStart() == target.getSelectionEnd()) { end = pDoc.getParagraphElement(target.getSelectionEnd()).getEndOffset(); } else { end = pDoc.getParagraphElement(target.getSelectionEnd() - 1).getEndOffset();
/** * If the selection is multi lined, then the full lines are selected, * otherwise, nothing is done. * @param target * @return true if the selection is multi-line, or a whole line */ public static boolean selectLines(JTextComponent target) { if (target.getSelectionStart() == target.getSelectionEnd()) { return false; } PlainDocument pDoc = (PlainDocument) target.getDocument(); Element es = pDoc.getParagraphElement(target.getSelectionStart()); // if more than one line is selected, we need to subtract one from the end // so that we do not select the line with the caret and no selection in it Element ee = pDoc.getParagraphElement(target.getSelectionEnd() - 1); if (es.equals(ee) && ee.getEndOffset() != target.getSelectionEnd()) { return false; } int start = es.getStartOffset(); int end = ee.getEndOffset(); target.select(start, end - 1); return true; }
/** * If the selection is multi lined, then the full lines are selected, * otherwise, nothing is done. * * @return true if the selection is multi-line, or a whole line */ public static boolean selectLines(JTextComponent target) { if (target.getSelectionStart() == target.getSelectionEnd()) { return false; } PlainDocument pDoc = (PlainDocument) target.getDocument(); Element es = pDoc.getParagraphElement(target.getSelectionStart()); // if more than one line is selected, we need to subtract one from the end // so that we do not select the line with the caret and no selection in it Element ee = pDoc.getParagraphElement(target.getSelectionEnd() - 1); if (es.equals(ee) && ee.getEndOffset() != target.getSelectionEnd()) { return false; } int start = es.getStartOffset(); int end = ee.getEndOffset(); target.select(start, end - 1); return true; }