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); } }
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(); Style style = textPane.addStyle("Blue", null); StyleConstants.setForeground(style, Color.blue);
StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("I'm a Style", null); StyleConstants.setForeground(style, Color.red);
public static void main(String[] args) { JTextPane jtp = new JTextPane(); StyledDocument doc = jtp.getStyledDocument(); Style style = jtp.addStyle("Red coloured text", null); StyleConstants.setForeground(style, Color.red); try { doc.insertString(doc.getLength(), "Style text",style); } catch (BadLocationException e){} StyleConstants.setForeground(style, Color.blue); try { doc.insertString(doc.getLength(), "Style text 2",style); } catch (BadLocationException e){} JFrame frame = new JFrame("ColourCheck"); frame.getContentPane().add(jtp); frame.pack(); frame.setVisible(true); }
private Style createStyle(final String name, final Style parent, final Color foreground, final Boolean bold, final Boolean italic) { final Style style = textPane.addStyle(name, parent); if (foreground != null) StyleConstants.setForeground(style, foreground); if (bold != null) StyleConstants.setBold(style, bold); if (italic != null) StyleConstants.setItalic(style, italic); return style; }
public Style getHighlightStyle() { if (matchedTextStyle == null) { Color highlightForegroundColor = Color.decode(theme.findProperty("text-highlight-foreground-color")); Color highlightBackgroundColor = Color.decode(theme.findProperty("text-highlight-background-color")); this.matchedTextStyle = pane.addStyle("highlight", null); StyleConstants.setForeground(this.matchedTextStyle, highlightForegroundColor); StyleConstants.setBackground(this.matchedTextStyle, highlightBackgroundColor); StyleConstants.setFontSize(this.matchedTextStyle, 14); } return matchedTextStyle; }
public Style getNormalTextStyle() { if (normalTextStyle == null) { Color foregroundColor = Color.decode(theme.findProperty("text-foreground-color")); Color backgroundColor = Color.decode(theme.findProperty("text-background-color")); this.normalTextStyle = pane.addStyle("normalText", null); StyleConstants.setForeground(this.normalTextStyle, foregroundColor); StyleConstants.setBackground(this.normalTextStyle, backgroundColor); StyleConstants.setFontSize(this.normalTextStyle, 14); return this.normalTextStyle; } return normalTextStyle; }
@Override public void actionPerformed(ActionEvent event) { Style style = AnnotationDisplayCustomizationFrame.this.styleMap .get(AnnotationDisplayCustomizationFrame.this.currentTypeName); if (style == null) { style = AnnotationDisplayCustomizationFrame.this.textPane.addStyle( AnnotationDisplayCustomizationFrame.this.currentTypeName, AnnotationDisplayCustomizationFrame.this.styleMap.get(CAS.TYPE_NAME_ANNOTATION)); } StyleConstants.setForeground(style, StyleConstants .getForeground(AnnotationDisplayCustomizationFrame.this.currentStyle)); StyleConstants.setBackground(style, StyleConstants .getBackground(AnnotationDisplayCustomizationFrame.this.currentStyle)); AnnotationDisplayCustomizationFrame.this.styleMap.put( AnnotationDisplayCustomizationFrame.this.currentTypeName, style); enableButtons(false); AnnotationDisplayCustomizationFrame.this.repaint(); }
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); } }
StyledDocument doc = pane.getStyledDocument(); Style style = pane.addStyle("Color Style", null); StyleConstants.setForeground(style, color); try {
public static void main(String[] args) { JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("Red Style", null); StyleConstants.setForeground(style, Color.red); try { doc.insertString(doc.getLength(), "This ", null); doc.insertString(doc.getLength(), "Apple", style); doc.insertString(doc.getLength(), " is a good ", null); doc.insertString(doc.getLength(), "Apple", style); } catch (BadLocationException e) { } JFrame frame = new JFrame("Test"); frame.getContentPane().add(textPane); frame.pack(); frame.setVisible(true); }
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);
/** * Sets the current style. * * @param style the new current style */ private void setCurrentStyle(Style style) { // Copy style. this.currentStyle = this.textPane.addStyle(currentStyleName, style); StyleConstants.setForeground(this.currentStyle, StyleConstants.getForeground(style)); StyleConstants.setBackground(this.currentStyle, StyleConstants.getBackground(style)); }
Document doc = this.textPane.getDocument(); Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style level0 = this.textPane.addStyle("level0", def); Style level1 = this.textPane.addStyle("level1", level0); StyleConstants.setBackground(level1, Color.green); Style level2 = this.textPane.addStyle("level2", level0); StyleConstants.setBackground(level2, Color.yellow); Style level3 = this.textPane.addStyle("level3", level0); StyleConstants.setBackground(level3, Color.orange); Style[] styleArray = { level0, level1, level2, level3 };
StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("I'm a Style", null); StyleConstants.setForeground(style, Color.red);
this.textPane.addStyle(defaultStyleName, defaultStyle); setCurrentStyle(style); this.fgColor = StyleConstants.getForeground(this.currentStyle);
JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("Style", null); StyleConstants.setForeground(style, Color.red); String word = "Hello"; if (word.equals("Hello")) { try { doc.insertString(doc.getLength(), word, style); } catch (BadLocationException ex) { ex.printStackTrace(); } } else { StyleConstants.setForeground(style, Color.blue); try { doc.insertString(doc.getLength(), word, style); } catch (BadLocationException e) { e.printStackTrace(); } }
JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("I'm a Style", null); StyleConstants.setForeground(style, Color.red); String word = "Hello"; if (word.equals("Hello") { try { doc.insertString(doc.getLength(), word, style); } catch (BadLocationException ex) { ex.printStackTrace(); } } else { StyleConstants.setForeground(style, Color.blue); try { doc.insertString(doc.getLength(), word, style); } catch (BadLocationException e) { e.printStackTrace(); } }
private void initStyle(SwingLayoutTheme theme) { Color highlightForegroundColor = Color.decode(theme.findProperty("text-highlight-foreground-color")); Color highlightBackgroundColor = Color.decode(theme.findProperty("text-highlight-background-color")); matchedTextStyle = description.addStyle("matchedText", null); StyleConstants.setForeground(matchedTextStyle, highlightForegroundColor); StyleConstants.setBackground(matchedTextStyle, highlightBackgroundColor); StyleConstants.setFontFamily(matchedTextStyle, FontManager.getInstance().getCurrentFontName()); StyleConstants.setFontSize(matchedTextStyle, 14); Color foregroundColor = Color.decode(theme.findProperty("text-foreground-color")); Color backgroundColor = Color.decode(theme.findProperty("text-background-color")); normalTextStyle = description.addStyle("normalText", null); StyleConstants.setForeground(normalTextStyle, foregroundColor); StyleConstants.setBackground(normalTextStyle, backgroundColor); StyleConstants.setFontFamily(normalTextStyle, FontManager.getInstance().getCurrentFontName()); StyleConstants.setFontSize(normalTextStyle, 14); }