JTextPane textPane = new JTextPane(); textPane.setText( "original text" ); StyledDocument doc = textPane.getStyledDocument(); // Define a keyword attribute SimpleAttributeSet keyWord = new SimpleAttributeSet(); StyleConstants.setForeground(keyWord, Color.RED); StyleConstants.setBackground(keyWord, Color.YELLOW); StyleConstants.setBold(keyWord, true); // Add some text try { doc.insertString(0, "Start of text\n", null ); doc.insertString(doc.getLength(), "\nEnd of text", keyWord ); } catch(Exception e) { System.out.println(e); }
public class Main { public static void main(String[] args) { JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("I'm a Style", null); StyleConstants.setForeground(style, Color.red); try { doc.insertString(doc.getLength(), "BLAH ",style); } catch (BadLocationException e){} StyleConstants.setForeground(style, Color.blue); try { doc.insertString(doc.getLength(), "BLEH",style); } catch (BadLocationException e){} JFrame frame = new JFrame("Test"); frame.getContentPane().add(textPane); frame.pack(); frame.setVisible(true); } }
public void actionPerformed(ActionEvent ae) { JTextComponent tComp = (JTextComponent) ae.getSource(); if (tComp.getDocument() instanceof StyledDocument) { doc = (StyledDocument) tComp.getDocument(); try { doc.getText(0, doc.getLength(), segment); } catch (Exception e) { // should NEVER reach here e.printStackTrace(); } int offset = tComp.getCaretPosition(); int index = findTabLocation(offset); buffer.delete(0, buffer.length()); buffer.append('\n'); if (index > -1) { for (int i = 0; i < index + 4; i++) { buffer.append(' '); } } try { doc.insertString(offset, buffer.toString(), doc.getDefaultRootElement().getAttributes()); } catch (BadLocationException ble) { ble.printStackTrace(); } } }
/** * Write a line to the Log section of the GUI * * @param text the string to log * @param color the color of the line */ private void appendLog(final String text, final Color color) { SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setForeground(sas, color); StyledDocument sd = logText.getStyledDocument(); try { synchronized (this) { sd.insertString(sd.getLength(), text + "\n", sas); } } catch (BadLocationException e) { } logText.setCaretPosition(sd.getLength()); }
void writeIndent(StyledDocument docu) throws BadLocationException { if (needIndent) { for (int i = 0; i < indent; i++) { docu.insertString(docu.getLength(), " ", null); } needIndent = false; } } }
/** * Add a colored string to the textpane, measure prompt at the same time. * @param string Text to append * @param color Color of text * @param isAddingPrompt Should we measure prompt length? */ private void appendPrompt(String string, Color color, boolean isAddingPrompt) { try { StyleConstants.setForeground(this.style, color); this.styledDocument.insertString(this.styledDocument.getLength(), string, this.style); if (isAddingPrompt) { this.prompt += string; } } catch (BadLocationException e) { LOGGER.error(e.getMessage(), e); } }
private void drawText(java.util.List<String> strings) { text.setText(""); StyledDocument doc = text.getStyledDocument(); try { for (String line : strings) { doc.insertString(doc.getLength(), line + '\n', doc.getStyle("regular")); } } catch (BadLocationException ble) { } text.setCaretPosition(0); }
private StyledDocument getDocument(InputStream inputStream, String encoding) { StyledDocument docu = new DefaultStyledDocument(); if (inputStream != null) { String data = getStringOfStream(inputStream, encoding); try { docu.insertString(0, data, null); } catch (BadLocationException e) { e.printStackTrace(); } } return docu; }
protected void drawText() { text.setText(""); StyledDocument doc = text.getStyledDocument(); try { for (String rule : getRules()) { doc.insertString(doc.getLength(), rule + '\n', doc.getStyle("small")); } } catch (BadLocationException e) { } text.setCaretPosition(0); }
docu.insertString(docu.getLength(), str + " ", NAME_STYLE); docu.insertString(docu.getLength(), str + " ", null); docu.insertString(docu.getLength(), "[ ", null); for (COSBase elem : (COSArray) obj) docu.insertString(docu.getLength(), "] ", null); docu.insertString(docu.getLength(), "(", null); byte[] bytes = ((COSString) obj).getBytes(); for (byte b : bytes) docu.insertString(docu.getLength(), str, ESCAPE_STYLE); docu.insertString(docu.getLength(), str, ESCAPE_STYLE); docu.insertString(docu.getLength(), str, STRING_STYLE); docu.insertString(docu.getLength(), ") ", null); docu.insertString(docu.getLength(), str + " ", NUMBER_STYLE); docu.insertString(docu.getLength(), "<< ", null); COSDictionary dict = (COSDictionary) obj; for (Map.Entry<COSName, COSBase> entry : dict.entrySet()) docu.insertString(docu.getLength(), ">> ", null);
docu.insertString(docu.getLength(), INLINE_IMAGE_BEGIN + "\n", OPERATOR_STYLE); COSDictionary dic = op.getImageParameters(); for (COSName key : dic.keySet()) docu.insertString(docu.getLength(), "/" + key.getName() + " ", null); writeToken(value, docu); docu.insertString(docu.getLength(), "\n", null); docu.insertString(docu.getLength(), IMAGE_DATA + "\n", INLINE_IMAGE_STYLE); docu.insertString(docu.getLength(), imageString, null); docu.insertString(docu.getLength(), "\n", null); docu.insertString(docu.getLength(), INLINE_IMAGE_END + "\n", OPERATOR_STYLE); docu.insertString(docu.getLength(), operator + "\n", OPERATOR_STYLE);
StyleConstants.setFontSize(normal, 72); StyleConstants.setForeground(normal, Color.blue); doc.insertString(doc.getLength(), "Test", normal); jtp.setSelectionStart(doc.getLength()); jtp.insertIcon(UIManager.getIcon("OptionPane.warningIcon"));
doc.insertString(doc.getLength(), " " + levelText + " ", levelStyle); doc.insertString(doc.getLength(), " [" + shortName + "]", nameStyle); doc.insertString(doc.getLength(), " " + message + "\n", null);
/** Inserts a text into given offset and marks it guarded. * @param doc document to insert to * @param offset offset of insertion * @param txt string text to insert * @exception NullPointerException If the <code>doc</code> parameter * is <code>null</code>. */ public static void insertGuarded(StyledDocument doc, int offset, String txt) throws BadLocationException { checkDocParameter(doc); doc.insertString(offset, txt, ATTR_ADD); }
private static void insertString(final StyledDocument doc, final String comment, final int last, final int start, final Style defStyle) { try { doc.insertString(doc.getLength(), comment.substring(last, start), defStyle); } catch (BadLocationException ex) { Support.LOG.log(Level.SEVERE, null, ex); } }
private void fillText() throws BadLocationException { textArea.setText(""); StyledDocument sd = textArea.getStyledDocument(); sd.insertString(0, "OtrosLogViewer\n", titleStyle); sd.insertString(sd.getLength(), "Build: " + build + "\n", mainStyle); sd.insertString(sd.getLength(), "Project web page: https://github.com/otros-systems/otroslogviewer/\n", mainStyle); sd.insertString(sd.getLength(), "Program documentation: https://github.com/otros-systems/otroslogviewer/wiki/Introduction?tm=6 \n", mainStyle); sd.insertString(sd.getLength(), "License: Apache Commons 2.0", licenceStyle); }
/** * Inserts the specified string in the specified document. * @param doc document in which to insert the text. * @param string text to insert. * @throws BadLocationException thrown if something wrong happened to the document. */ private static void insertNormalString(StyledDocument doc, String string) throws BadLocationException { doc.insertString(doc.getLength(), string + LINE_BREAK, doc.getStyle(STYLE_NORMAL)); }
/** * Inserts a line break in the specified document. * @param doc document in which to insert the text. * @throws BadLocationException thrown if something wrong happened to the document. */ private static void insertLineBreak(StyledDocument doc) throws BadLocationException { doc.insertString(doc.getLength(), LINE_BREAK, doc.getStyle(STYLE_NORMAL)); }
/** * Inserts the specified URL in the specified document. * @param doc document in which to insert the text. * @param url url to insert. * @throws BadLocationException thrown if something wrong happened to the document. */ private static void insertUrl(StyledDocument doc, String url) throws BadLocationException { doc.insertString(doc.getLength(), " ", doc.getStyle(STYLE_NORMAL)); doc.insertString(doc.getLength(), url + LINE_BREAK, doc.getStyle(STYLE_URL)); }
try { // Get the text pane's document JTextPane textPane = new JTextPane(); StyledDocument doc = (StyledDocument)textPane.getDocument(); // The image must first be wrapped in a style Style style = doc.addStyle("StyleName", null); StyleConstants.setIcon(style, new ImageIcon("imagefile")); // Insert the image at the end of the text doc.insertString(doc.getLength(), "ignored text", style); } catch (BadLocationException e) { }