Java Code Examples for javax.swing.text.StyleConstants#setForeground()

The following examples show how to use javax.swing.text.StyleConstants#setForeground() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DisplayArea.java    From android-classyshark with Apache License 2.0 6 votes vote down vote up
@Override
public void displayError() {
    displayDataState = DisplayDataState.ERROR;
    clearText();

    style = jTextPane.addStyle("STYLE", null);
    Document doc = jTextPane.getStyledDocument();

    try {
        StyleConstants.setForeground(style, theme.getDefaultColor());
        StyleConstants.setFontSize(style, 15);
        StyleConstants.setFontFamily(style, "Monospaced");

        doc.insertString(doc.getLength(), "\n\n\n\t\t\t There was a problem loading the class  ", style);
        doc.insertString(doc.getLength(), Doodle.get(), style);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    jTextPane.setDocument(doc);
}
 
Example 2
Source File: JConsole.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
private AttributeSet setStyle(
        String fontFamilyName, int size, Color color) {
    MutableAttributeSet attr = new SimpleAttributeSet();
    if (color != null) {
        StyleConstants.setForeground(attr, color);
    }
    if (fontFamilyName != null) {
        StyleConstants.setFontFamily(attr, fontFamilyName);
    }
    if (size != -1) {
        StyleConstants.setFontSize(attr, size);
    }

    setStyle(attr);

    return getStyle();
}
 
Example 3
Source File: ColorPane.java    From patchwork-patcher with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void append(Color color, String string) {
	if (color != null) {
		StyleConstants.setForeground(oneStyleToRuleThemAll, color);
	}

	try {
		this.getDocument().insertString(this.getDocument().getLength(), string, oneStyleToRuleThemAll);
	} catch (BadLocationException e) {
		PatchworkUI.LOGGER.throwing(Level.ERROR, e);
	}
}
 
Example 4
Source File: JConsole.java    From COMP6237 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private AttributeSet setStyle(
		String fontFamilyName,
		int size,
		Color color,
		boolean bold,
		boolean italic,
		boolean underline
		)
{
	final MutableAttributeSet attr = new SimpleAttributeSet();
	if (color != null)
		StyleConstants.setForeground(attr, color);
	if (fontFamilyName != null)
		StyleConstants.setFontFamily(attr, fontFamilyName);
	if (size != -1)
		StyleConstants.setFontSize(attr, size);
	StyleConstants.setBold(attr, bold);
	StyleConstants.setItalic(attr, italic);
	StyleConstants.setUnderline(attr, underline);

	setStyle(attr);

	return getStyle();
}
 
Example 5
Source File: ColorsManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static AttributeSet getUnusedLocalVariableAttributes () {
    if (unusedLocalVariableAttributeSet == null) {
        SimpleAttributeSet sas = new SimpleAttributeSet ();
        StyleConstants.setForeground (sas, new Color (115, 115, 115));
        unusedLocalVariableAttributeSet = sas;
    }
    return unusedLocalVariableAttributeSet;
}
 
Example 6
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout(5, 5));
  JButton ok = new JButton("Test");
  ok.addActionListener(e -> append("Test test test test", true));

  JButton err = new JButton("Error");
  err.addActionListener(e -> append("Error error error error", false));

  JButton clr = new JButton("Clear");
  clr.addActionListener(e -> jtp.setText(""));

  Box box = Box.createHorizontalBox();
  box.add(Box.createHorizontalGlue());
  box.add(ok);
  box.add(err);
  box.add(Box.createHorizontalStrut(5));
  box.add(clr);

  jtp.setEditable(false);
  StyledDocument doc = jtp.getStyledDocument();
  // Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  Style def = doc.getStyle(StyleContext.DEFAULT_STYLE);

  // Style regular = doc.addStyle("regular", def);
  // StyleConstants.setForeground(def, Color.BLACK);

  Style error = doc.addStyle("error", def);
  StyleConstants.setForeground(error, Color.RED);

  JScrollPane scroll = new JScrollPane(jtp);
  scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  scroll.getVerticalScrollBar().setUnitIncrement(25);

  add(scroll);
  add(box, BorderLayout.SOUTH);
  setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 7
Source File: ColorsManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static AttributeSet getUnusedFieldAttributes () {
    if (unusedFieldAttributeSet == null) {
        SimpleAttributeSet sas = new SimpleAttributeSet ();
        StyleConstants.setForeground (sas, new Color (115, 115, 115));
        StyleConstants.setBold (sas, true);
        unusedFieldAttributeSet = sas;
    }
    return unusedFieldAttributeSet;
}
 
Example 8
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  MutableAttributeSet attr = new SimpleAttributeSet();
  StyleConstants.setForeground(attr, Color.RED);
  StyleConstants.setFontSize(attr, 32);

  MutableAttributeSet a = new SimpleAttributeSet();
  StyleConstants.setLineSpacing(a, .5f);
  // StyleConstants.setSpaceAbove(a, 5f);
  // StyleConstants.setSpaceBelow(a, 5f);
  // StyleConstants.setLeftIndent(a, 5f);
  // StyleConstants.setRightIndent(a, 5f);
  JTextPane editor1 = new JTextPane();
  editor1.setParagraphAttributes(a, false);
  setDummyText(editor1, attr);

  // StyleSheet styleSheet = new StyleSheet();
  // styleSheet.addRule("body {font-size: 24pt; line-height: 2.0}"); // XXX
  // HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
  // htmlEditorKit.setStyleSheet(styleSheet);
  // editor1.setEditorKit(htmlEditorKit);
  // editor1.setText("<html><body>12341234<br />***<br />111<font size='32'>12341234<br />999</font></body></html>");

  JTextPane editor2 = new JTextPane();
  editor2.setEditorKit(new BottomInsetEditorKit());
  setDummyText(editor2, attr);

  JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  sp.setTopComponent(new JScrollPane(editor1));
  sp.setBottomComponent(new JScrollPane(editor2));
  sp.setResizeWeight(.5);
  add(sp);
  setPreferredSize(new Dimension(320, 240));
}
 
Example 9
Source File: StyledConsoleLogger.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
public void applyLogLevelStyle(LogLevel level, LogLevelStyle logLevelStyle) {
    Style style = context.getStyle(level.value());

    Color foreground = GuiUtil.hexToColor(logLevelStyle.getForeground());
    if (foreground != null)
        StyleConstants.setForeground(style, foreground);
    else
        style.removeAttribute(StyleConstants.Foreground);

    Color background = GuiUtil.hexToColor(logLevelStyle.getBackground());
    if (background != null)
        StyleConstants.setBackground(style, background);
    else
        style.removeAttribute(StyleConstants.Background);
}
 
Example 10
Source File: EditableNotificationMessageElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void updateStyle(@Nonnull JEditorPane editorPane, @javax.annotation.Nullable JTree tree, Object value, boolean selected, boolean hasFocus) {
  super.updateStyle(editorPane, tree, value, selected, hasFocus);

  final HTMLDocument htmlDocument = (HTMLDocument)editorPane.getDocument();
  final Style linkStyle = htmlDocument.getStyleSheet().getStyle(LINK_STYLE);
  StyleConstants.setForeground(linkStyle, IdeTooltipManager.getInstance().getLinkForeground(false));
  StyleConstants.setItalic(linkStyle, true);
  HTMLDocument.Iterator iterator = htmlDocument.getIterator(HTML.Tag.A);
  while (iterator.isValid()) {
    boolean disabledLink = false;
    final AttributeSet attributes = iterator.getAttributes();
    if (attributes instanceof SimpleAttributeSet) {
      final Object attribute = attributes.getAttribute(HTML.Attribute.HREF);
      if (attribute instanceof String && disabledLinks.containsKey(attribute)) {
        disabledLink = true;
        //TODO [Vlad] add support for disabled link text update
        ////final String linkText = disabledLinks.get(attribute);
        //if (linkText != null) {
        //}
        ((SimpleAttributeSet)attributes).removeAttribute(HTML.Attribute.HREF);
      }
      if (attribute == null) {
        disabledLink = true;
      }
    }
    if (!disabledLink) {
      htmlDocument.setCharacterAttributes(
              iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset(), linkStyle, false);
    }
    iterator.next();
  }
}
 
Example 11
Source File: ResultsTextPane.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
public void appendColoredText(String text, Color color) {
  StyledDocument doc = getStyledDocument();

  Style style = addStyle("Color Style", null);
  StyleConstants.setForeground(style, color);
  try {
    doc.insertString(doc.getLength(), text, style);
  } catch (BadLocationException e) {
    e.printStackTrace();
  }
}
 
Example 12
Source File: ResultsTextPane.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
private void createStyles() {
  error = this.addStyle("error", null);
  StyleConstants.setForeground(error, Color.red);
  succed = this.addStyle("succed", null);
  StyleConstants.setForeground(succed, Color.green);
  info = this.addStyle("succed", null);
  StyleConstants.setForeground(info, Color.black);
}
 
Example 13
Source File: GuiUtil.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
public static void addStyle(JTextPane textPane, String name,
                            Color foreground, Color background,
                            boolean bold)
{
    StyledDocument doc = textPane.getStyledDocument();
    StyleContext context = StyleContext.getDefaultStyleContext();
    Style def = context.getStyle(StyleContext.DEFAULT_STYLE);
    Style style = doc.addStyle(name, def);
    if (foreground != null)
        StyleConstants.setForeground(style, foreground);
    if (background != null)
        StyleConstants.setBackground(style, background);
    StyleConstants.setBold(style, bold);
}
 
Example 14
Source File: ResultsTextPane.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
public void appendColoredText(String text, Color color) {
  StyledDocument doc = getStyledDocument();

  Style style = addStyle("Color Style", null);
  StyleConstants.setForeground(style, color);
  try {
    doc.insertString(doc.getLength(), text, style);
  } catch (BadLocationException e) {
    e.printStackTrace();
  }
}
 
Example 15
Source File: ResultsTextPane.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
private void createStyles() {
  error = this.addStyle("error", null);
  StyleConstants.setForeground(error, Color.red);
  succed = this.addStyle("succed", null);
  StyleConstants.setForeground(succed, Color.green);
  info = this.addStyle("succed", null);
  StyleConstants.setForeground(info, Color.black);
}
 
Example 16
Source File: StrategyCanvas.java    From iMetrica with GNU General Public License v3.0 4 votes vote down vote up
public void addSameLineText(String text, Color col)
{
   StyleConstants.setForeground(main_style, col);
   try { doc.insertString(doc.getLength(), " " + text + " ", main_style); }
   catch (BadLocationException e){}
}
 
Example 17
Source File: StrategyCanvas.java    From iMetrica with GNU General Public License v3.0 4 votes vote down vote up
public void addTextNewLine(String text, Color col)
{
   StyleConstants.setForeground(main_style, col);
   try { doc.insertString(doc.getLength(), text + "\n", main_style); }
   catch (BadLocationException e){}
}
 
Example 18
Source File: KTextEdit.java    From stendhal with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Initializes the basic styles.
 *
 * @param textPane
 *            the active text component
 * @param mainTextSize size of regular text
 */
protected void initStylesForTextPane(final JTextPane textPane, int mainTextSize) {
	// ****** General style definitions for the text pane ******
	Style regular = textPane.getStyle("regular");
	if (regular == null) {
		final Style def = StyleContext.getDefaultStyleContext().getStyle(
				StyleContext.DEFAULT_STYLE);
		regular = textPane.addStyle("regular", def);
		StyleConstants.setFontFamily(def, "Dialog");
	}
	StyleConstants.setFontSize(regular, mainTextSize);

	Style s = textPane.getStyle("normal");
	if (s == null) {
		s = textPane.addStyle("normal", regular);
		StyleConstants.setBold(s, true);
		StyleConstants.setForeground(s, HEADER_COLOR);
	}

	s = textPane.getStyle("bold");
	if (s == null) {
		s = textPane.addStyle("bold", regular);
		StyleConstants.setItalic(s, true);
		StyleConstants.setBold(s, true);
		StyleConstants.setForeground(s, Color.blue);
	}
	StyleConstants.setFontSize(regular, mainTextSize + 1);

	s = textPane.getStyle("header");
	if (s == null) {
		s = textPane.addStyle("header", regular);
		StyleConstants.setItalic(s, true);
		StyleConstants.setForeground(s, HEADER_COLOR);
	}
	StyleConstants.setFontSize(s, mainTextSize);

	s = textPane.getStyle("timestamp");
	if (s == null) {
		s = textPane.addStyle("timestamp", regular);
		StyleConstants.setItalic(s, true);
		StyleConstants.setForeground(s, HEADER_COLOR);
	}
	StyleConstants.setFontSize(s, mainTextSize - 1);

	//****** Styles used by the string formatter ******
	StyleSet defaultAttributes = new StyleSet(StyleContext.getDefaultStyleContext(), regular);

	StyleSet attributes = defaultAttributes.copy();
	attributes.setAttribute(StyleConstants.Italic, Boolean.TRUE);
	attributes.setAttribute(StyleConstants.Foreground, Color.blue);
	attributes.setAttribute("linkact", new LinkListener());

	formatter.addStyle('#', attributes);

	attributes = defaultAttributes.copy();
	attributes.setAttribute(StyleConstants.Underline, Boolean.TRUE);
	formatter.addStyle('ยง', attributes);
}
 
Example 19
Source File: MatchingHighlighter.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void createHighlightedStyleByParen(String p) {
    Style style = StyleContext.getDefaultStyleContext().addStyle(highlightedStyleName(p), findStyleByTokenType(PAREN_MAP.get(p).getV1()));
    StyleConstants.setForeground(style, Color.YELLOW.darker());
    StyleConstants.setBold(style, true);
}
 
Example 20
Source File: GroovyFilter.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void init() {
    StyleContext styleContext = StyleContext.getDefaultStyleContext();
    Style defaultStyle = styleContext.getStyle(StyleContext.DEFAULT_STYLE);

    Style comment = styleContext.addStyle(COMMENT, defaultStyle);
    StyleConstants.setForeground(comment, COMMENT_COLOR);
    StyleConstants.setItalic(comment, true);

    Style quotes = styleContext.addStyle(QUOTES, defaultStyle);
    StyleConstants.setForeground(quotes, Color.MAGENTA.darker().darker());

    Style charQuotes = styleContext.addStyle(SINGLE_QUOTES, defaultStyle);
    StyleConstants.setForeground(charQuotes, Color.GREEN.darker().darker());

    Style slashyQuotes = styleContext.addStyle(SLASHY_QUOTES, defaultStyle);
    StyleConstants.setForeground(slashyQuotes, Color.ORANGE.darker());

    Style digit = styleContext.addStyle(DIGIT, defaultStyle);
    StyleConstants.setForeground(digit, Color.RED.darker());

    Style operation = styleContext.addStyle(OPERATION, defaultStyle);
    StyleConstants.setBold(operation, true);

    Style ident = styleContext.addStyle(IDENT, defaultStyle);

    Style reservedWords = styleContext.addStyle(RESERVED_WORD, defaultStyle);
    StyleConstants.setBold(reservedWords, true);
    StyleConstants.setForeground(reservedWords, Color.BLUE.darker().darker());

    Style leftParens = styleContext.addStyle(IDENT, defaultStyle);

    getRootNode().putStyle(SLASH_STAR_COMMENT, comment);
    getRootNode().putStyle(SLASH_SLASH_COMMENT, comment);
    getRootNode().putStyle(QUOTES, quotes);
    getRootNode().putStyle(SINGLE_QUOTES, charQuotes);
    getRootNode().putStyle(SLASHY_QUOTES, slashyQuotes);

    getRootNode().putStyle(new String[] {
        HEX_INTEGER_LITERAL,
        OCTAL_INTEGER_LITERAL,
        BINARY_INTEGER_LITERAL,
        DECIMAL_FLOATING_POINT_LITERAL,
        HEXADECIMAL_FLOATING_POINT_LITERAL,
        DECIMAL_INTEGER_LITERAL,
    }, digit);

    getRootNode().putStyle(OPERATION, operation);
    StructuredSyntaxDocumentFilter.LexerNode node = createLexerNode();
    node.putStyle(RESERVED_WORDS, reservedWords);
    node.putStyle(LEFT_PARENS, leftParens);
    getRootNode().putChild(OPERATION, node);

    getRootNode().putStyle(IDENT, ident);
    node = createLexerNode();
    node.putStyle(RESERVED_WORDS, reservedWords);
    getRootNode().putChild(IDENT, node);
}