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

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

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

StyledDocument.insertString介绍

暂无

代码示例

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

  1. JTextPane textPane = new JTextPane();
  2. textPane.setText( "original text" );
  3. StyledDocument doc = textPane.getStyledDocument();
  4. // Define a keyword attribute
  5. SimpleAttributeSet keyWord = new SimpleAttributeSet();
  6. StyleConstants.setForeground(keyWord, Color.RED);
  7. StyleConstants.setBackground(keyWord, Color.YELLOW);
  8. StyleConstants.setBold(keyWord, true);
  9. // Add some text
  10. try
  11. {
  12. doc.insertString(0, "Start of text\n", null );
  13. doc.insertString(doc.getLength(), "\nEnd of text", keyWord );
  14. }
  15. catch(Exception e) { System.out.println(e); }

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

  1. public class Main {
  2. public static void main(String[] args) {
  3. JTextPane textPane = new JTextPane();
  4. StyledDocument doc = textPane.getStyledDocument();
  5. Style style = textPane.addStyle("I'm a Style", null);
  6. StyleConstants.setForeground(style, Color.red);
  7. try { doc.insertString(doc.getLength(), "BLAH ",style); }
  8. catch (BadLocationException e){}
  9. StyleConstants.setForeground(style, Color.blue);
  10. try { doc.insertString(doc.getLength(), "BLEH",style); }
  11. catch (BadLocationException e){}
  12. JFrame frame = new JFrame("Test");
  13. frame.getContentPane().add(textPane);
  14. frame.pack();
  15. frame.setVisible(true);
  16. }
  17. }

代码示例来源: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: RipMeApp/ripme

  1. /**
  2. * Write a line to the Log section of the GUI
  3. *
  4. * @param text the string to log
  5. * @param color the color of the line
  6. */
  7. private void appendLog(final String text, final Color color) {
  8. SimpleAttributeSet sas = new SimpleAttributeSet();
  9. StyleConstants.setForeground(sas, color);
  10. StyledDocument sd = logText.getStyledDocument();
  11. try {
  12. synchronized (this) {
  13. sd.insertString(sd.getLength(), text + "\n", sas);
  14. }
  15. } catch (BadLocationException e) { }
  16. logText.setCaretPosition(sd.getLength());
  17. }

代码示例来源:origin: apache/pdfbox

  1. void writeIndent(StyledDocument docu) throws BadLocationException
  2. {
  3. if (needIndent)
  4. {
  5. for (int i = 0; i < indent; i++)
  6. {
  7. docu.insertString(docu.getLength(), " ", null);
  8. }
  9. needIndent = false;
  10. }
  11. }
  12. }

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

  1. /**
  2. * Add a colored string to the textpane, measure prompt at the same time.
  3. * @param string Text to append
  4. * @param color Color of text
  5. * @param isAddingPrompt Should we measure prompt length?
  6. */
  7. private void appendPrompt(String string, Color color, boolean isAddingPrompt) {
  8. try {
  9. StyleConstants.setForeground(this.style, color);
  10. this.styledDocument.insertString(this.styledDocument.getLength(), string, this.style);
  11. if (isAddingPrompt) {
  12. this.prompt += string;
  13. }
  14. } catch (BadLocationException e) {
  15. LOGGER.error(e.getMessage(), e);
  16. }
  17. }

代码示例来源:origin: magefree/mage

  1. private void drawText(java.util.List<String> strings) {
  2. text.setText("");
  3. StyledDocument doc = text.getStyledDocument();
  4. try {
  5. for (String line : strings) {
  6. doc.insertString(doc.getLength(), line + '\n', doc.getStyle("regular"));
  7. }
  8. } catch (BadLocationException ble) {
  9. }
  10. text.setCaretPosition(0);
  11. }

代码示例来源:origin: apache/pdfbox

  1. private StyledDocument getDocument(InputStream inputStream, String encoding)
  2. {
  3. StyledDocument docu = new DefaultStyledDocument();
  4. if (inputStream != null)
  5. {
  6. String data = getStringOfStream(inputStream, encoding);
  7. try
  8. {
  9. docu.insertString(0, data, null);
  10. }
  11. catch (BadLocationException e)
  12. {
  13. e.printStackTrace();
  14. }
  15. }
  16. return docu;
  17. }

代码示例来源:origin: magefree/mage

  1. protected void drawText() {
  2. text.setText("");
  3. StyledDocument doc = text.getStyledDocument();
  4. try {
  5. for (String rule : getRules()) {
  6. doc.insertString(doc.getLength(), rule + '\n', doc.getStyle("small"));
  7. }
  8. } catch (BadLocationException e) {
  9. }
  10. text.setCaretPosition(0);
  11. }

代码示例来源:origin: apache/pdfbox

  1. docu.insertString(docu.getLength(), str + " ", NAME_STYLE);
  2. docu.insertString(docu.getLength(), str + " ", null);
  3. docu.insertString(docu.getLength(), "[ ", null);
  4. for (COSBase elem : (COSArray) obj)
  5. docu.insertString(docu.getLength(), "] ", null);
  6. docu.insertString(docu.getLength(), "(", null);
  7. byte[] bytes = ((COSString) obj).getBytes();
  8. for (byte b : bytes)
  9. docu.insertString(docu.getLength(), str, ESCAPE_STYLE);
  10. docu.insertString(docu.getLength(), str, ESCAPE_STYLE);
  11. docu.insertString(docu.getLength(), str, STRING_STYLE);
  12. docu.insertString(docu.getLength(), ") ", null);
  13. docu.insertString(docu.getLength(), str + " ", NUMBER_STYLE);
  14. docu.insertString(docu.getLength(), "<< ", null);
  15. COSDictionary dict = (COSDictionary) obj;
  16. for (Map.Entry<COSName, COSBase> entry : dict.entrySet())
  17. docu.insertString(docu.getLength(), ">> ", null);

代码示例来源:origin: apache/pdfbox

  1. docu.insertString(docu.getLength(), INLINE_IMAGE_BEGIN + "\n", OPERATOR_STYLE);
  2. COSDictionary dic = op.getImageParameters();
  3. for (COSName key : dic.keySet())
  4. docu.insertString(docu.getLength(), "/" + key.getName() + " ", null);
  5. writeToken(value, docu);
  6. docu.insertString(docu.getLength(), "\n", null);
  7. docu.insertString(docu.getLength(), IMAGE_DATA + "\n", INLINE_IMAGE_STYLE);
  8. docu.insertString(docu.getLength(), imageString, null);
  9. docu.insertString(docu.getLength(), "\n", null);
  10. docu.insertString(docu.getLength(), INLINE_IMAGE_END + "\n", OPERATOR_STYLE);
  11. docu.insertString(docu.getLength(), operator + "\n", OPERATOR_STYLE);

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

  1. StyleConstants.setFontSize(normal, 72);
  2. StyleConstants.setForeground(normal, Color.blue);
  3. doc.insertString(doc.getLength(), "Test", normal);
  4. jtp.setSelectionStart(doc.getLength());
  5. jtp.insertIcon(UIManager.getIcon("OptionPane.warningIcon"));

代码示例来源:origin: apache/pdfbox

  1. doc.insertString(doc.getLength(), " " + levelText + " ", levelStyle);
  2. doc.insertString(doc.getLength(), " [" + shortName + "]", nameStyle);
  3. doc.insertString(doc.getLength(), " " + message + "\n", null);

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

  1. /** Inserts a text into given offset and marks it guarded.
  2. * @param doc document to insert to
  3. * @param offset offset of insertion
  4. * @param txt string text to insert
  5. * @exception NullPointerException If the <code>doc</code> parameter
  6. * is <code>null</code>.
  7. */
  8. public static void insertGuarded(StyledDocument doc, int offset, String txt)
  9. throws BadLocationException {
  10. checkDocParameter(doc);
  11. doc.insertString(offset, txt, ATTR_ADD);
  12. }

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

  1. private static void insertString(final StyledDocument doc, final String comment, final int last, final int start, final Style defStyle) {
  2. try {
  3. doc.insertString(doc.getLength(), comment.substring(last, start), defStyle);
  4. } catch (BadLocationException ex) {
  5. Support.LOG.log(Level.SEVERE, null, ex);
  6. }
  7. }

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

  1. private void fillText() throws BadLocationException {
  2. textArea.setText("");
  3. StyledDocument sd = textArea.getStyledDocument();
  4. sd.insertString(0, "OtrosLogViewer\n", titleStyle);
  5. sd.insertString(sd.getLength(), "Build: " + build + "\n", mainStyle);
  6. sd.insertString(sd.getLength(), "Project web page: https://github.com/otros-systems/otroslogviewer/\n", mainStyle);
  7. sd.insertString(sd.getLength(), "Program documentation: https://github.com/otros-systems/otroslogviewer/wiki/Introduction?tm=6 \n", mainStyle);
  8. sd.insertString(sd.getLength(), "License: Apache Commons 2.0", licenceStyle);
  9. }

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

  1. /**
  2. * Inserts the specified string in the specified document.
  3. * @param doc document in which to insert the text.
  4. * @param string text to insert.
  5. * @throws BadLocationException thrown if something wrong happened to the document.
  6. */
  7. private static void insertNormalString(StyledDocument doc, String string) throws BadLocationException {
  8. doc.insertString(doc.getLength(), string + LINE_BREAK, doc.getStyle(STYLE_NORMAL));
  9. }

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

  1. /**
  2. * Inserts a line break in the specified document.
  3. * @param doc document in which to insert the text.
  4. * @throws BadLocationException thrown if something wrong happened to the document.
  5. */
  6. private static void insertLineBreak(StyledDocument doc) throws BadLocationException {
  7. doc.insertString(doc.getLength(), LINE_BREAK, doc.getStyle(STYLE_NORMAL));
  8. }

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

  1. /**
  2. * Inserts the specified URL in the specified document.
  3. * @param doc document in which to insert the text.
  4. * @param url url to insert.
  5. * @throws BadLocationException thrown if something wrong happened to the document.
  6. */
  7. private static void insertUrl(StyledDocument doc, String url) throws BadLocationException {
  8. doc.insertString(doc.getLength(), " ", doc.getStyle(STYLE_NORMAL));
  9. doc.insertString(doc.getLength(), url + LINE_BREAK, doc.getStyle(STYLE_URL));
  10. }

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

  1. try {
  2. // Get the text pane's document
  3. JTextPane textPane = new JTextPane();
  4. StyledDocument doc = (StyledDocument)textPane.getDocument();
  5. // The image must first be wrapped in a style
  6. Style style = doc.addStyle("StyleName", null);
  7. StyleConstants.setIcon(style, new ImageIcon("imagefile"));
  8. // Insert the image at the end of the text
  9. doc.insertString(doc.getLength(), "ignored text", style);
  10. } catch (BadLocationException e) {
  11. }

相关文章