@Override public String doInBackground() { if (openfired != null) { // Get the input stream and read from it try (InputStream in = openfired.getErrorStream()) { int c; while ((c = in.read()) != -1) { try { StyleConstants.setForeground(styles, Color.red); pane.getDocument().insertString(pane.getDocument().getLength(), "" + (char)c, styles); } catch (BadLocationException e) { // Ignore. } } } catch (IOException e) { e.printStackTrace(); } } return "ok"; } };
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; }
private void buildContentPanel() { editorPane = new JEditorPane (); editorPane.setContentType("text/rtf"); editorPane.addKeyListener(new InputListener()); // defaultAttrSet = ((StyledEditorKit)editorPane.getEditorKit()).getInputAttributes(); StyleConstants.setFontFamily(defaultAttrSet, "Lucida Sans"); Document doc = new DefaultStyledDocument(); editorPane.setDocument(doc); try { doc.insertString(0, initText, defaultAttrSet); } catch (Exception ex) { throw new RuntimeException(ex); } JScrollPane scrollPane = new JScrollPane(editorPane); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); editorPane.setEditable(true); }
private AttributeSet getAttributeSet(String tag) { MutableAttributeSet attr = new SimpleAttributeSet(); Color color = tagToColorMap.get(tag); StyleConstants.setBackground(attr, color); StyleConstants.setForeground(attr, Color.WHITE); return attr; }
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); }
public static void main(String args[]) throws BadLocationException { JFrame jf = new JFrame("StyledText"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cp = jf.getContentPane(); JTextPane pane = new JTextPane(); SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setBold(set, true); pane.setCharacterAttributes(set, true); pane.setText("Eine "); set = new SimpleAttributeSet(); StyleConstants.setItalic(set, true); StyleConstants.setForeground(set, Color.red); StyleConstants.setBackground(set, Color.blue); Document doc = pane.getStyledDocument(); doc.insertString(doc.getLength(), "Kleine ", set); set = new SimpleAttributeSet(); StyleConstants.setFontSize(set, 24); doc.insertString(doc.getLength(), "Nachtmusic", set); cp.add(scrollPane, BorderLayout.CENTER); jf.setSize(400, 300);
public void run() { for (int i = 0; i < fnt.length; i++) { StyleConstants.setBold(mas, false); StyleConstants.setItalic(mas, false); StyleConstants.setFontFamily(mas, fnt[i]); StyleConstants.setFontSize(mas, 16); dis(fnt[i]); try { e.printStackTrace(); StyleConstants.setBold(mas, true); dis(fnt[i] + " Bold"); try { e.printStackTrace(); StyleConstants.setItalic(mas, true); dis(fnt[i] + " Bold & Italic"); try { e.printStackTrace(); StyleConstants.setBold(mas, false); dis(fnt[i] + " Italic"); try {
textPane1 = new JTextPane(); black = new SimpleAttributeSet(); red = new SimpleAttributeSet(); StyleConstants.setForeground(black, Color.black); StyleConstants.setForeground(red, Color.red); textPane1.setEditorKit(new StyledEditorKit()); doc1 = textPane1.getDocument(); StyledDocument styledDocument = textPane1.getStyledDocument(); Element element; for(int i=0; i<styledDocument.getLength();i++) { element = styledDocument.getCharacterElement(i); AttributeSet attributeNew = element.getAttributes(); System.out.println(i); append2(styledDocument.getText(i, 1), attributeNew); doc1.insertString(doc1.getLength(), text, attribute); } catch (BadLocationException ex) { ex.printStackTrace(); frame2 = new JFrame("frame 2"); frame2.setSize(400, 300); frame2.setLocation(300,0); frame2.getContentPane().add(new JScrollPane(textPane2), BorderLayout.CENTER); frame2.setVisible(true);
JFrame frame = new JFrame(); JTextPane pane = new JTextPane();; JButton button = new JButton("Button"); addColoredText(pane, "Blue Text\n", Color.BLUE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane.setPreferredSize(new Dimension(200, 200)); frame.getContentPane().add(pane, BorderLayout.CENTER); frame.getContentPane().add(button, BorderLayout.WEST); frame.pack(); frame.setVisible(true); StyledDocument doc = pane.getStyledDocument(); Style style = pane.addStyle("Color Style", null); StyleConstants.setForeground(style, color); try { doc.insertString(doc.getLength(), text, style);
JFrame f = new JFrame("Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String s = new Date().toString(); JTextPane jtp = new JTextPane(); StyledDocument doc = (StyledDocument) jtp.getDocument(); SimpleAttributeSet normal = new SimpleAttributeSet(); StyleConstants.setFontFamily(normal, "SansSerif"); StyleConstants.setFontSize(normal, 16); SimpleAttributeSet boldBlue = new SimpleAttributeSet(normal); StyleConstants.setBold(boldBlue, true); StyleConstants.setForeground(boldBlue, Color.blue); SimpleAttributeSet highAlert = new SimpleAttributeSet(boldBlue); StyleConstants.setFontSize(highAlert, 18); StyleConstants.setItalic(highAlert, true); StyleConstants.setForeground(highAlert, Color.red); doc.insertString(doc.getLength(), s + "\n", normal); doc.insertString(doc.getLength(), s + "\n", boldBlue); doc.insertString(doc.getLength(), s + "\n", highAlert); f.add(jtp); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
JFrame frame = new JFrame(); JPanel panel = new JPanel(); JTextPane textPane = new JTextPane(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.setLayout(new BorderLayout()); panel.setPreferredSize(new Dimension(200, 200)); panel.add(textPane, BorderLayout.CENTER); panel.add(button, BorderLayout.SOUTH); textPane.addStyle("negra", null); StyledDocument doc = textPane.getStyledDocument(); int start = textPane.getSelectionStart(); int end = textPane.getSelectionEnd(); if (StyleConstants.isBold(style)) { StyleConstants.setBold(style, false); } else { StyleConstants.setBold(style, true); doc.setCharacterAttributes(start, end - start, style, false); frame.add(panel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextPane textPane = new JTextPane(doc); textPane.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has " + "been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of " for (int i = 0; i < textPane.getDocument().getLength(); i++) { SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setForeground(set, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); StyleConstants.setFontSize(set, random.nextInt(12) + 12); StyleConstants.setBold(set, random.nextBoolean()); StyleConstants.setItalic(set, random.nextBoolean()); StyleConstants.setUnderline(set, random.nextBoolean()); doc.setCharacterAttributes(i, 1, set, true); frame.add(new JScrollPane(textPane)); frame.setSize(500, 400); frame.setVisible(true);
JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet left = new SimpleAttributeSet(); StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT); StyleConstants.setForeground(left, Color.RED); SimpleAttributeSet right = new SimpleAttributeSet(); StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT); StyleConstants.setForeground(right, Color.BLUE); doc.insertString(doc.getLength(), "Are you busy tonight?", left ); doc.setParagraphAttributes(doc.getLength(), 1, left, false); doc.insertString(doc.getLength(), "\nNo", right ); doc.setParagraphAttributes(doc.getLength(), 1, right, false); doc.insertString(doc.getLength(), "\nFeel like going to a movie?", left ); doc.setParagraphAttributes(doc.getLength(), 1, left, false); doc.insertString(doc.getLength(), "\nSure", right ); doc.setParagraphAttributes(doc.getLength(), 1, right, false); JFrame frame = new JFrame("Text Pane Chat"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( new JScrollPane( textPane ) ); frame.setLocationByPlatform( true ); frame.setSize(200, 200); frame.setVisible( true );
import javax.swing.JFrame; import javax.swing.JTextPane; import javax.swing.WindowConstants; import javax.swing.text.Style; import javax.swing.text.StyleConstants; public class BoldSelected { public static void main(final String[] args) { new BoldSelected().launchGui(); } private void launchGui() { final String title = "Set bold font style for selected text in JTextArea instance"; final JFrame frame = new JFrame("Stack Overflow: " + title); frame.setBounds(100, 100, 800, 600); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); final JTextPane textPane = new JTextPane(); textPane.setText(title + "."); final Style style = textPane.addStyle("Bold", null); StyleConstants.setBold(style, true); textPane.getStyledDocument().setCharacterAttributes(4, 33, style, false); frame.getContentPane().add(textPane); frame.setVisible(true); } }
final JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttonPanel = new JPanel(); buttonPanel.add(new JButton(new AbstractAction("Update") { @Override public void actionPerformed(ActionEvent e) { for (JTextPane jtp : textPane) { StyledDocument doc = (StyledDocument) jtp.getDocument(); SimpleAttributeSet style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Serif"); StyleConstants.setFontSize(style, size++); try { doc.insertString(doc.getLength(), " one two three", style); Dimension d = jtp.getPreferredSize(); Rectangle r = jtp.modelToView(jtp.getDocument().getLength()); d.height = r.y + r.height; jtp.setPreferredSize(d); ex.printStackTrace(System.err); frame.pack(); frame.add(new JScrollPane(textPanel)); frame.pack(); frame.setVisible(true);
JFrame frame = new JFrame("ScaledJTextPane using BufferedImage"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextPane tp = new JTextPane(); tp.setFont(new Font(FONT, Font.PLAIN, SIZE)); tp.setPreferredSize(new Dimension(400, 300)); StyledDocument doc = tp.getStyledDocument(); Style defaultStyle = StyleContext.getDefaultStyleContext() .getStyle(StyleContext.DEFAULT_STYLE); Style boldStyle = doc.addStyle("bold", defaultStyle); StyleConstants.setBold(boldStyle, true); String boldText = "Four score and seven years ago "; String plainText = "our fathers brought forth on this continent, " + "proposition that all men are created equal."; try { doc.insertString(doc.getLength(), boldText, boldStyle); doc.insertString(doc.getLength(), plainText, defaultStyle); } catch (BadLocationException ble) { ble.printStackTrace(System.err); panel.add(tp);
JFrame frame = new JFrame(); DefaultStyledDocument document = new DefaultStyledDocument(); JTextPane pane = new JTextPane(document); JPanel mainPanel = new JPanel(); JButton button = new JButton("Button"); button.setPreferredSize(new Dimension(100, 40)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane.setPreferredSize(new Dimension(200, 200)); mainPanel.add(button); frame.getContentPane().add(pane, BorderLayout.CENTER); frame.getContentPane().add(mainPanel, BorderLayout.WEST); StyleContext context = new StyleContext(); StyleConstants.setForeground(style, Color.BLACK); document.insertString(0, "Four: success \n", style); StyleConstants.setForeground(style, Color.RED); document.insertString(0, "Three: error \n", style); document.insertString(0, "Two: error \n", style); StyleConstants.setForeground(style, Color.BLACK); frame.pack(); frame.setVisible(true);
SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setForeground(sas, Color.RED); int end = textpane.getText().indexOf(' ', index + 1); if (end == -1) { end = textpane.getDocument().getLength(); reachedEnd = true; ((StyledDocument) textpane.getDocument()).setCharacterAttributes(index, end - index, sas, true); try { Rectangle modelToView = textpane.modelToView(end); frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); doc = new DefaultStyledDocument(); textpane = new JTextPane(doc); textpane.setText(TEXT); frame.add(new JScrollPane(textpane)); frame.setSize(300, 300); frame.setVisible(true);
"one two three four five\r\n"; JTextPane textPane = new JTextPane(); textPane.setText(text); JScrollPane scrollPane = new JScrollPane( textPane ); getContentPane().add( scrollPane ); StyledDocument doc = textPane.getStyledDocument(); SimpleAttributeSet keyWord = new SimpleAttributeSet(); StyleConstants.setBackground(keyWord, Color.CYAN); int length = textPane.getDocument().getLength(); text = textPane.getDocument().getText(0, length); doc.setCharacterAttributes(offset, search.length(), keyWord, false); offset += search.length(); throws Exception JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new TextAndNewLinesTest(); frame.setTitle("Text and New Lines"); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.setSize(400, 120); frame.setLocationRelativeTo( null ); frame.setVisible(true);
private JTextPane textPane = new JTextPane(); setSize(300, 200); SimpleAttributeSet red = new SimpleAttributeSet(); StyleConstants.setForeground(red, Color.red); StyleConstants.setBold(red, true); SimpleAttributeSet blue = new SimpleAttributeSet(); StyleConstants.setForeground(blue, Color.blue); SimpleAttributeSet italic = new SimpleAttributeSet(); StyleConstants.setItalic(italic, true); StyleConstants.setForeground(italic, Color.orange); append("red", red); Container content = getContentPane(); content.add(new JScrollPane(textPane), BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Document d = textPane.getDocument(); try { d.insertString(d.getLength(), s, attributes); } catch (BadLocationException ble) { new jeditorfont().setVisible(true);