/** * Highlights specified text region by changing the character attributes */ private void highlightText(int start, int end, SimpleAttributeSet style) { if (start < end) { textPane.getStyledDocument().setCharacterAttributes(start, end - start + 1, style, 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); }
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); } }
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; }
/** * 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()); }
pane.setParagraphAttributes(set, false); StyledDocument doc = pane.getStyledDocument(); final Style alertStyle = doc.addStyle("Alert", null); StyleConstants.setForeground(alertStyle, Color.red);
protected void addStylesToDocument(JTextPane outputArea) { StyledDocument doc = outputArea.getStyledDocument(); Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style regular = doc.addStyle("regular", def); StyleConstants.setFontFamily(def, "Monospaced"); promptStyle = doc.addStyle("prompt", regular); StyleConstants.setForeground(promptStyle, Color.BLUE); commandStyle = doc.addStyle("command", regular); StyleConstants.setForeground(commandStyle, Color.MAGENTA); outputStyle = doc.addStyle("output", regular); StyleConstants.setBold(outputStyle, true); }
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); }
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); }
jtp = new JTextPane(); jtp.setText("\ntype some text in the above empty line and check the wrapping behavior"); doc = jtp.getStyledDocument(); doc.addDocumentListener(new DocumentListener() {
public void log(String name, String level, Object o, Throwable throwable) StyledDocument doc = textPane.getStyledDocument();
doc = jta.getStyledDocument(); jsp = new JScrollPane(jta); jsp.setPreferredSize(new Dimension(height, width));
/** * Creates new form Card * * @param card * @param bigCard * @param dimension * @param gameId */ public Card(CardView card, BigCard bigCard, CardDimensions dimension, UUID gameId) { this.dimension = dimension; initComponents(); this.gameId = gameId; this.card = card; this.bigCard = bigCard; small = new BufferedImage(Config.dimensions.frameWidth, Config.dimensions.frameHeight, BufferedImage.TYPE_INT_RGB); backgroundName = getBackgroundName(); background = ImageHelper.getBackground(card, backgroundName); StyledDocument doc = text.getStyledDocument(); Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style regular = doc.addStyle("regular", def); StyleConstants.setFontFamily(def, "arial"); Style s = doc.addStyle("small", regular); StyleConstants.setFontSize(s, 9); //addMouseListener(this); //text.addMouseListener(this); //addFocusListener(this); //addMouseMotionListener(this); //text.addMouseMotionListener(this); //addComponentListener(this); }
JTextPane textPane = new JTextPane(); Style bold = textPane.addStyle("bold", null); StyleConstants.setBold(bold, true); textPane.setText("I'll be bold."); textPane.getStyledDocument().setCharacterAttributes(8, 4, bold, true);
JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet style = new SimpleAttributeSet(); StyleConstants.setLeftIndent(style, 20); StyleConstants.setFirstLineIndent(style, -20); StyleConstants.setForeground(style, Color.BLUE); doc.setParagraphAttributes(0, doc.getLength(), style, true);
private void prepareTextPane() { resetStyles(); parent.setBorder(null); parent.getStyledDocument().addDocumentListener( new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { handleDocumentUpdated(); } @Override public void removeUpdate(DocumentEvent e) { handleDocumentUpdated(); } @Override public void changedUpdate(DocumentEvent e) { /* NO-OP */ } } ); }
@Override public void mouseMoved(MouseEvent e) { int pos = previewText.viewToModel(e.getPoint()); StyledDocument doc = previewText.getStyledDocument(); Element element = doc.getCharacterElement(pos); AttributeSet addtributes = element.getAttributes(); Style style = doc.getStyle((String) addtributes .getAttribute(StyleConstants.NameAttribute)); previewText.setToolTipText( "Click to change the " + style.getName() + " color"); } });
private static Style getStyleAtPoint(JTextPane text, Point point) { int pos = text.viewToModel(point); StyledDocument doc = text.getStyledDocument(); Element element = doc.getCharacterElement(pos); AttributeSet addtributes = element.getAttributes(); return doc.getStyle((String) addtributes .getAttribute(StyleConstants.NameAttribute)); }
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); }
protected void addStylesToDocument(JTextPane outputArea) { StyledDocument doc = outputArea.getStyledDocument(); Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style regular = doc.addStyle("regular", def); StyleConstants.setFontFamily(def, "Monospaced"); promptStyle = doc.addStyle("prompt", regular); StyleConstants.setForeground(promptStyle, Color.BLUE); commandStyle = doc.addStyle("command", regular); StyleConstants.setForeground(commandStyle, Color.MAGENTA); outputStyle = doc.addStyle("output", regular); StyleConstants.setBold(outputStyle, true); }