java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to add colored text to the document E-mail
User Rating: / 49
PoorBest 

This Java Swing tip illustrates a method of adding colored-text to the document. This tip is a simple extension of JTextPane that allows the user to easily append colored text to the document.


Image

import javax.swing.*;
import javax.swing.text.*;
import java.awt.Color;

public class ColorPane extends JTextPane {

  public void appendNaive(Color c, String s) { // naive implementation
    // bad: instiantiates a new AttributeSet object on each call
    SimpleAttributeSet aset = new SimpleAttributeSet();
    StyleConstants.setForeground(aset, c);
  
    int len = getText().length();
    setCaretPosition(len)// place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s)// there is no selection, so inserts at caret
  }

  public void append(Color c, String s) { // better implementation--uses StyleContext
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                                        StyleConstants.Foreground, c);
  
    int len = getDocument().getLength()// same value as getText().length();
    setCaretPosition(len);  // place caret at the end (with no selection)
    setCharacterAttributes(aset, false);
    replaceSelection(s)// there is no selection, so inserts at caret
  }

  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(600400);
    f.setVisible(true);
  }

  public static boolean isPrime(int n) {
    if (n < 2return false;
    double max = Math.sqrt(n);
    for (int j=2; j <= max; j+=1)
      if (n % j == 0return 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);
  }

}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.