StyleConstants: setForeground(MutableAttributeSet a, Color fg) : StyleConstants « javax.swing.text « Java by API






StyleConstants: setForeground(MutableAttributeSet a, Color fg)

 
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class MainClass{
  public static void main(String argv[]) {

    ColorPane pane = new ColorPane();
    for (int n = 1; n <= 400; n += 1) {
      if (isPrime(n)) {
        pane.append(Color.red, String.valueOf(n) + ' ');
      } else if (isPerfectSquare(n)) {
        pane.append(Color.blue, String.valueOf(n) + ' ');
      } else {
        pane.append(Color.black, String.valueOf(n) + ' ');
      }
    }

    JFrame f = new JFrame("ColorPane example");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new JScrollPane(pane));
    f.setSize(600, 400);
    f.setVisible(true);
  }

  public static boolean isPrime(int n) {
    if (n < 2)
      return false;
    double max = Math.sqrt(n);
    for (int j = 2; j <= max; j += 1)
      if (n % j == 0)
        return false; // j is a factor
    return true;
  }

  public static boolean isPerfectSquare(int n) {
    int j = 1;
    while (j * j < n && j * j > 0)
      j += 1;
    return (j * j == n);
  }

  
}

class ColorPane extends JTextPane {

  public void appendNaive(Color c, String s) {
    SimpleAttributeSet aset = new SimpleAttributeSet();
    StyleConstants.setForeground(aset, c);

    int len = getText().length();
    setCaretPosition(len); 
    setCharacterAttributes(aset, false);
    replaceSelection(s);
  }

  public void append(Color c, String s) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

    int len = getDocument().getLength();
    setCaretPosition(len); 
    setCharacterAttributes(aset, false);
    replaceSelection(s); 
  }


}

           
         
  








Related examples in the same category

1.StyleConstants.Bold
2.StyleConstants.Foreground
3.StyleConstants.Italic
4.StyleConstants.LineSpacing
5.StyleConstants: setAlignment(MutableAttributeSet a, int align)
6.StyleConstants: setBold(MutableAttributeSet a, boolean b)
7.StyleConstants: setComponent(MutableAttributeSet a, Component c)
8.StyleConstants: setFirstLineIndent(MutableAttributeSet a, float i)
9.StyleConstants: setFontFamily(MutableAttributeSet a, String fam)
10.StyleConstants: setFontSize(MutableAttributeSet a, int s)
11.StyleConstants: setIcon(MutableAttributeSet a, Icon c)
12.StyleConstants: setItalic(MutableAttributeSet a, boolean b)
13.StyleConstants: setLeftIndent(MutableAttributeSet a, float i)
14.StyleConstants: setRightIndent(MutableAttributeSet a, float i)
15.StyleConstants: setSpaceAbove(MutableAttributeSet a, float i)
16.StyleConstants: setSpaceBelow(MutableAttributeSet a, float i)
17.StyleConstants: setTabSet(MutableAttributeSet a, TabSet tabs)
18.StyleConstants: setUnderline(MutableAttributeSet a, boolean b)