javax.swing.text.StyledDocument.getText()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(136)

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

StyledDocument.getText介绍

暂无

代码示例

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

  1. public void actionPerformed(ActionEvent ae) {
  2. JTextComponent tComp = (JTextComponent) ae.getSource();
  3. if (tComp.getDocument() instanceof StyledDocument) {
  4. doc = (StyledDocument) tComp.getDocument();
  5. try {
  6. doc.getText(0, doc.getLength(), segment);
  7. }
  8. catch (Exception e) {
  9. // should NEVER reach here
  10. e.printStackTrace();
  11. }
  12. int offset = tComp.getCaretPosition();
  13. int index = findTabLocation(offset);
  14. buffer.delete(0, buffer.length());
  15. buffer.append('\n');
  16. if (index > -1) {
  17. for (int i = 0; i < index + 4; i++) {
  18. buffer.append(' ');
  19. }
  20. }
  21. try {
  22. doc.insertString(offset, buffer.toString(),
  23. doc.getDefaultRootElement().getAttributes());
  24. }
  25. catch (BadLocationException ble) {
  26. ble.printStackTrace();
  27. }
  28. }
  29. }

代码示例来源:origin: beryx/text-io

  1. public String getPartialInput() {
  2. try {
  3. return document.getText(startReadLen, document.getLength() - startReadLen);
  4. } catch (BadLocationException e) {
  5. logger.error("Failed to retrieve partial input text", e);
  6. return "";
  7. }
  8. }

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-javascript2-nodejs

  1. @Override
  2. public void run() {
  3. try {
  4. docContentRef.set(document.getText(0, document.getLength()));
  5. } catch (BadLocationException ex) {
  6. bleRef.set(ex);
  7. }
  8. }

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

  1. String html = "<html>...";
  2. JTextPane pane = new JTextPane();
  3. pane.setContentType("text/html");
  4. pane.setText(html);
  5. StyledDocument doc = pane.getStyledDocument();
  6. try {
  7. System.out.println("Text: " + doc.getText(0, doc.getLength()));
  8. } catch (BadLocationException ex) {
  9. Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
  10. }

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

  1. @Override
  2. public void run() {
  3. try {
  4. int position = doc.getText(0, doc.getLength()).indexOf(find);
  5. // if element wasn't found - it isn't likely new project with sample index.html page and
  6. // there is probably not wished to have it changed, so don't do it
  7. if (position >= 0) {
  8. doc.insertString(position, enhanceBy + "\n", null); //NOI18N
  9. }
  10. } catch (BadLocationException ex) {
  11. Exceptions.printStackTrace(ex);
  12. }
  13. }
  14. });

代码示例来源:origin: otros-systems/otroslogviewer

  1. private void scrollToSearchResult(ArrayList<String> toHighlight, JTextPane textPane) {
  2. if (toHighlight.size() == 0) {
  3. return;
  4. }
  5. try {
  6. StyledDocument logDetailsDocument = textPane.getStyledDocument();
  7. String text = logDetailsDocument.getText(0, logDetailsDocument.getLength());
  8. String string = toHighlight.get(0);
  9. textPane.setCaretPosition(Math.max(text.indexOf(string), 0));
  10. } catch (BadLocationException e) {
  11. LOGGER.warn("Cant scroll to content, wrong location: " + e.getMessage());
  12. }
  13. }

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

  1. StyledDocument document = textPane.getStyledDocument();
  2. Style highlight = document.addStyle("highlight", null);
  3. StyleConstants.setBackground(highlight, Color.YELLOW);
  4. String text = document.getText(0, document.getLength());
  5. while ((index = text.indexOf(myWord, index)) >= 0) {
  6. document.setCharacterAttributes(index, myWord.length(), highlight, false);
  7. index += myWord.length();
  8. }

代码示例来源:origin: org.netbeans.api/org-openide-text

  1. public void run() {
  2. // Part of #33165 - the following code is wrapped by doc.render()
  3. try {
  4. int p = position.getOffset();
  5. if (p >= doc.getLength()) {
  6. retStringArray[0] = "";
  7. } else {
  8. retStringArray[0] = doc.getText(position.getOffset(), getLength());
  9. }
  10. } catch (BadLocationException ex) {
  11. Logger.getLogger(DocumentLine.class.getName()).log(Level.WARNING, null, ex);
  12. retStringArray[0] = null;
  13. }
  14. // End of the code wrapped by doc.render()
  15. }
  16. }

代码示例来源:origin: org.apache.uima/uimaj-ep-cas-editor-ide

  1. private String convert(InputStream rtfDocumentInputStream) throws IOException {
  2. RTFEditorKit aRtfEditorkit = new RTFEditorKit();
  3. StyledDocument styledDoc = new DefaultStyledDocument();
  4. String textDocument;
  5. try {
  6. aRtfEditorkit.read(rtfDocumentInputStream, styledDoc, 0);
  7. textDocument = styledDoc.getText(0, styledDoc.getLength());
  8. } catch (BadLocationException e) {
  9. throw new IOException("Error during parsing");
  10. }
  11. return textDocument;
  12. }

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-debugger-common2

  1. /**
  2. * Map document position to contents of whole line at that position.
  3. * SHOULD be combined with addrFromLine to givew:
  4. * Address viewToModel(pos)
  5. */
  6. private String getLineAt(int pos) {
  7. // By default a "paragraph" is a line
  8. Element pp = styledDoc.getParagraphElement(pos);
  9. int startPos = pp.getStartOffset();
  10. int endPos = pp.getEndOffset();
  11. int length = endPos - startPos;
  12. String line = null;
  13. try {
  14. line = styledDoc.getText(startPos, length);
  15. } catch (Exception x) {
  16. }
  17. return line;
  18. }

代码示例来源:origin: org.netbeans.api/org-openide-text

  1. /** Finds the text contained in this range.
  2. * @return the text
  3. * @exception IOException if any I/O problem occurred during document loading (if that was necessary)
  4. * @exception BadLocationException if the positions are out of the bounds of the document
  5. */
  6. public String getText() throws BadLocationException, IOException {
  7. StyledDocument doc = begin.getCloneableEditorSupport().openDocument();
  8. int p1 = begin.getOffset();
  9. int p2 = end.getOffset();
  10. return doc.getText(p1, p2 - p1);
  11. }

代码示例来源:origin: org.opentcs.thirdparty.jhotdraw/jhotdraw

  1. @Override
  2. public Figure createTextArea(double x, double y, double w, double h, StyledDocument doc, Map<AttributeKey, Object> attributes) {
  3. SVGTextAreaFigure figure = new SVGTextAreaFigure();
  4. figure.setBounds(new Point2D.Double(x,y),new Point2D.Double(x+w,y+h));
  5. try {
  6. figure.setText(doc.getText(0, doc.getLength()));
  7. } catch (BadLocationException e) {
  8. InternalError ex = new InternalError(e.getMessage());
  9. ex.initCause(e);
  10. throw ex;
  11. }
  12. figure.setAttributes(attributes);
  13. return figure;
  14. }

代码示例来源:origin: org.netbeans.api/org-openide-text

  1. public void run() {
  2. // Part of #33165 - the following code is wrapped by doc.render()
  3. int lineNumber = getLineNumber();
  4. int lineStart = NbDocument.findLineOffset(doc, lineNumber);
  5. // #24434: Check whether the next line exists
  6. // (the current one could be the last one).
  7. int lineEnd;
  8. if ((lineNumber + 1) >= NbDocument.findLineRootElement(doc).getElementCount()) {
  9. lineEnd = doc.getLength();
  10. } else {
  11. lineEnd = NbDocument.findLineOffset(doc, lineNumber + 1);
  12. }
  13. try {
  14. retStringArray[0] = doc.getText(lineStart, lineEnd - lineStart);
  15. } catch (BadLocationException ex) {
  16. Logger.getLogger(DocumentLine.class.getName()).log(Level.WARNING, null, ex);
  17. retStringArray[0] = null;
  18. }
  19. // End of the code wrapped by doc.render()
  20. }
  21. }

代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/openide

  1. /** Finds the text contained in this range.
  2. * @return the text
  3. * @exception IOException if any I/O problem occurred during document loading (if that was necessary)
  4. * @exception BadLocationException if the positions are out of the bounds of the document
  5. */
  6. public String getText() throws BadLocationException, IOException {
  7. StyledDocument doc = begin.getCloneableEditorSupport().openDocument();
  8. int p1 = begin.getOffset();
  9. int p2 = end.getOffset();
  10. return doc.getText(p1, p2 - p1);
  11. }

代码示例来源:origin: otros-systems/otroslogviewer

  1. public static void highlightSearchResult(
  2. OtrosJTextWithRulerScrollPane<JTextPane> otrosJTextWithRulerScrollPane,
  3. PluginableElementsContainer<MessageColorizer> colorizersContainer,
  4. Theme theme) {
  5. MessageUpdateUtils messageUpdateUtils = new MessageUpdateUtils();
  6. StyledDocument styledDocument = otrosJTextWithRulerScrollPane.getjTextComponent().getStyledDocument();
  7. String text;
  8. try {
  9. text = styledDocument.getText(0, styledDocument.getLength());
  10. } catch (BadLocationException e) {
  11. LOGGER.error("Cant get document text for log details view: ", e);
  12. return;
  13. }
  14. MessageColorizer messageColorizer = colorizersContainer.getElement(SearchResultColorizer.class.getName());
  15. List<MessageFragmentStyle> messageFragmentStyles = new ArrayList<>();
  16. if (messageColorizer != null) {
  17. messageFragmentStyles.addAll(messageUpdateUtils.colorizeMessageWithTimeLimit(text, 0, messageColorizer, 10));
  18. }
  19. markSearchResult(messageFragmentStyles, otrosJTextWithRulerScrollPane, theme);
  20. }

代码示例来源:origin: net.sf.squirrel-sql.thirdpary-non-maven/openide

  1. /** Finds the text contained in this range.
  2. * @return the text
  3. * @exception IOException if any I/O problem occurred during document loading (if that was necessary)
  4. * @exception BadLocationException if the positions are out of the bounds of the document
  5. */
  6. public String getText() throws BadLocationException, IOException {
  7. StyledDocument doc = begin.getCloneableEditorSupport().openDocument();
  8. int p1 = begin.getOffset();
  9. int p2 = end.getOffset();
  10. return doc.getText(p1, p2 - p1);
  11. }

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-team-commons

  1. private void registerForIssueLinks(final JTextPane pane, final Link issueLink, final IssueRefProvider issueIdProvider) {
  2. pane.removeMouseMotionListener(motionListener);
  3. try {
  4. String text = "";
  5. try {
  6. text = pane.getStyledDocument().getText(0, pane.getStyledDocument().getLength());
  7. } catch (BadLocationException ex) {
  8. Support.LOG.log(Level.INFO, null, ex);
  9. }
  10. registerLinkIntern(pane, issueIdProvider.getIssueRefSpans(text), issueLink);
  11. } catch (Exception ex) {
  12. Exceptions.printStackTrace(ex);
  13. }
  14. pane.addMouseMotionListener(motionListener);
  15. }

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

  1. static int getRowFirstNonWhite(StyledDocument doc, int offset)
  2. throws BadLocationException {
  3. Element lineElement = doc.getParagraphElement(offset);
  4. int start = lineElement.getStartOffset();
  5. while (start + 1 < lineElement.getEndOffset()) {
  6. try {
  7. if (doc.getText(start, 1).charAt(0) != ' ') {
  8. break;
  9. }
  10. } catch (BadLocationException ex) {
  11. throw (BadLocationException) new BadLocationException(
  12. "calling getText(" + start + ", " + (start + 1)
  13. + ") on doc of length: " + doc.getLength(), start).initCause(ex);
  14. }
  15. start++;
  16. }
  17. return start;
  18. }

代码示例来源:origin: org.opentcs.thirdparty.jhotdraw/jhotdraw

  1. @Override
  2. public Figure createText(Point2D.Double[] coordinates, double[] rotates, StyledDocument text, Map<AttributeKey, Object> a) {
  3. SVGTextFigure figure = new SVGTextFigure();
  4. figure.setCoordinates(coordinates);
  5. figure.setRotates(rotates);
  6. try {
  7. figure.setText(text.getText(0, text.getLength()));
  8. } catch (BadLocationException e) {
  9. InternalError ex = new InternalError(e.getMessage());
  10. ex.initCause(e);
  11. throw ex;
  12. }
  13. figure.setAttributes(a);
  14. return figure;
  15. }

代码示例来源:origin: net.sf.squirrel-sql.thirdpary-non-maven/openide

  1. doc.render(new Runnable() { public void run() {
  2. // Part of #33165 - the following code is wrapped by doc.render()
  3. int lineNumber = getLineNumber();
  4. int lineStart = NbDocument.findLineOffset(doc, lineNumber);
  5. // #24434: Check whether the next line exists
  6. // (the current one could be the last one).
  7. int lineEnd;
  8. if((lineNumber + 1)
  9. >= NbDocument.findLineRootElement(doc).getElementCount()) {
  10. lineEnd = doc.getLength();
  11. } else {
  12. lineEnd = NbDocument.findLineOffset(doc, lineNumber + 1);
  13. }
  14. try {
  15. retStringArray[0] = doc.getText(lineStart, lineEnd - lineStart);
  16. } catch (BadLocationException ex) {
  17. ErrorManager.getDefault ().notify ( ErrorManager.EXCEPTION, ex);
  18. retStringArray[0] = null;
  19. }
  20. // End of the code wrapped by doc.render()
  21. }});
  22. return retStringArray[0];

相关文章