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(); } } }
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); }
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()); }
JFrame frame = new JFrame(TestTextPane.class.getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextPane textPane2 = new JTextPane(); textPane2.setText("12345"); frame.add(textPane2); JButton button = new JButton("Make it red"); button.addActionListener(new ActionListener() { final SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setForeground(set, Color.RED); int p0 = textPane2.getSelectionStart(); int p1 = textPane2.getSelectionEnd(); if (p0 != p1) { StyledDocument doc = textPane2.getStyledDocument(); doc.setCharacterAttributes(p0, p1 - p0, set, false); public void run() { textPane2.getCaret().setDot(textPane2.getText().length()); MutableAttributeSet inputAttributes = textPane2.getInputAttributes(); inputAttributes.addAttributes(set); frame.add(button, BorderLayout.SOUTH); frame.setSize(600, 400); frame.setVisible(true);
JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextPane jtp = new JTextPane(); StyledDocument doc = (StyledDocument) jtp.getDocument(); SimpleAttributeSet normal = new SimpleAttributeSet(); StyleConstants.setFontFamily(normal, "Serif"); 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()); f.add(jtp); f.pack(); f.setLocationRelativeTo(null);
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);
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); } }
@Override public void run() { JFrame frame = new JFrame("Colored Text"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textPane.setText("Different Colored Text"); for (int i = 0; i < textPane.getDocument().getLength(); i++) { SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setForeground(set, new Color(random.nextInt(256), random.nextInt(256), StyleConstants.setFontSize(set, random.nextInt(12) + 12); StyleConstants.setBold(set, random.nextBoolean()); doc.setCharacterAttributes(i, 1, set, true); frame.add(new JScrollPane(textPane)); frame.pack(); 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 " 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);
public void run() { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); JTextPane jta = new JTextPane(); SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setBackground(sas, Color.RED); StyledDocument doc = jta.getStyledDocument(); doc.setCharacterAttributes(wordsStartPos[i], words[i].length(), sas, false); frame.add(jta); frame.pack(); frame.setVisible(true);
public void run() { JFrame frame = initgui(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); JFrame frame = new JFrame("Test"); JPanel panel = new JPanel(); StyledDocument doc = (StyledDocument) new DefaultStyledDocument(); JTextPane textpane = new JTextPane(doc); textpane.setText("Test"); javax.swing.text.Style style = textpane.addStyle("Red", null); StyleConstants.setForeground(style, Color.RED); doc.setCharacterAttributes(0, 1, textpane.getStyle("Red"), true); panel.add(textpane); frame.add(panel); return frame;
private JFrame frame = new JFrame(); private JTextPane jtp; private StyledDocument doc; 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() { Style defaultStyle = jtp.getStyle(StyleContext.DEFAULT_STYLE); doc.setCharacterAttributes(0, doc.getLength(), defaultStyle, false); JScrollPane scroll = new JScrollPane(jtp); scroll.setPreferredSize(new Dimension(200, 200)); frame.add(scroll); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); 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); 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);
extends JFrame { JTextPane p = new JTextPane(); public Smiley() throws Exception { p.setEditorKit(new StyledEditorKit()); getContentPane().add(p, BorderLayout.CENTER); SimpleAttributeSet attrs = new SimpleAttributeSet(); StyleConstants.setIcon(attrs, getImage()); p.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { int start = 0; while (index > -1) { Element el = doc.getCharacterElement(index); if (StyleConstants.getIcon(el.getAttributes()) == null) { doc.remove(index, 2); SimpleAttributeSet attrs = new SimpleAttributeSet(); StyleConstants.setIcon(attrs, getImage()); doc.insertString(index, ":)", attrs); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400, 400); test11.show();
final JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); final MutableAttributeSet standard = new SimpleAttributeSet(); StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, 0, standard, true); MutableAttributeSet keyWord = new SimpleAttributeSet(); StyleConstants.setForeground(keyWord, Color.red); StyleConstants.setItalic(keyWord, true); textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n"); doc.setCharacterAttributes(0, 3, keyWord, false); doc.setCharacterAttributes(19, 4, keyWord, false); try { doc.insertString(0, "Start of text\n", null); } catch (Exception e) { StyledDocument doc = textPane.getStyledDocument(); doc.setCharacterAttributes(index, 4, selWord, false); add(toggleButton, BorderLayout.SOUTH); public void run() { TextPaneAttributes frame = new TextPaneAttributes(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true);
frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); private JTextPane textPane = new JTextPane(); this.add(textPane, BorderLayout.CENTER); JPanel panel = new JPanel(); panel.add(btnStyle); this.add(panel, BorderLayout.NORTH); btnStyle.addActionListener(new ActionListener() { @Override Element element = doc.getCharacterElement(start); AttributeSet as = element.getAttributes(); MutableAttributeSet asNew = new SimpleAttributeSet(as.copyAttributes()); StyleConstants.setBold(asNew, !StyleConstants.isBold(as)); doc.setCharacterAttributes(start, textPane.getSelectedText().length(), asNew, true); String text = (StyleConstants.isBold(as) ? "Cancel Bold" : "Bold"); btnStyle.setText(text); boolean isBold = StyleConstants.isBold(as) ? false : true;
setSize(800, 600); initUI(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); textpane = new JTextPane(); textpane.setPreferredSize(new Dimension(700, 600)); Style style = document.addStyle("StyleName", null); StyleConstants.setIcon(style, new ImageIcon(image)); document.insertString(document.getLength(), "ignored text", style); } catch (Exception e){ panel.add(textpane); add(panel); pack(); public void run() { MathNotesPlus app = new MathNotesPlus(); app.setVisible(true);