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); } }
StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, doc.getLength(), center, false);
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); }
/** * 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()); }
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(); } } }
private JComponent createNoPlannerFoundTextField() { String infoMessage = "No planner benchmarks have been found in the benchmarkDirectory (" + benchmarkAggregator.getBenchmarkDirectory() + ")."; JTextPane textPane = new JTextPane(); textPane.setEditable(false); textPane.setText(infoMessage); // center info message StyledDocument styledDocument = textPane.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); StyleConstants.setBold(center, true); styledDocument.setParagraphAttributes(0, styledDocument.getLength(), center, false); return textPane; }
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); }
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")); jtp.setSelectionStart(doc.getLength()); jtp.insertComponent(new JLabel("Label")); jtp.setSelectionStart(doc.getLength());
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);
/** * 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 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 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)); }
/** * Inserts the specified string and details in the specified document. * @param doc document in which to insert the text. * @param string text to insert. * @param details details that will be added to the text. * @throws BadLocationException thrown if something wrong happened to the document. */ private static void insertDetailedString(StyledDocument doc, String string, String details) throws BadLocationException { doc.insertString(doc.getLength(), string + " ", doc.getStyle(STYLE_NORMAL)); doc.insertString(doc.getLength(), "(" + details + ")" + LINE_BREAK, doc.getStyle(STYLE_DETAILS)); }
private void resetStyles(StyledDocument doc) { doc.setParagraphAttributes(0, doc.getLength(), plainStyle, true); StyleConstants.setFontSize(fontSizeStyle, getFontSize()); Font f = OWLRendererPreferences.getInstance().getFont(); StyleConstants.setFontFamily(fontSizeStyle, f.getFamily()); doc.setParagraphAttributes(0, doc.getLength(), fontSizeStyle, false); setupFont(); }
public void run() { try { addRemoveDocListener(getDoc(), false); getDoc().remove(0, getDoc().getLength()); addRemoveDocListener(getDoc(), true); } catch (BadLocationException ble) { ERR.log(Level.INFO, null, ble); } } }