Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-27-2008, 08:48 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
JTextPane Styles Example
Code:
import javax.swing.*; import javax.swing.text.*; import java.awt.*; public class StylesExample1 { public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) {} JFrame f = new JFrame("Styles Example 1"); // Create the StyleContext, the document and the pane StyleContext sc = new StyleContext(); final DefaultStyledDocument doc = new DefaultStyledDocument(sc); JTextPane pane = new JTextPane(doc); // Create and add the style final Style heading2Style = sc.addStyle("Heading2", null); heading2Style.addAttribute(StyleConstants.Foreground, Color.red); heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16)); heading2Style.addAttribute(StyleConstants.FontFamily, "serif"); heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true)); try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { try { // Add the text to the document doc.insertString(0, text, null); // Finally, apply the style to the heading doc.setParagraphAttributes(0, 1, heading2Style, false); } catch (BadLocationException e) { } } }); } catch (Exception e) { System.out.println("Exception when constructing document: " + e); System.exit(1); } f.getContentPane().add(new JScrollPane(pane)); f.setSize(400, 300); f.setVisible(true); } public static final String text = "Attributes, Styles and Style Contexts\n" + "The simple PlainDocument class that you saw in the previous " + "chapter is only capable of holding text. The more complex text " + "components use a more sophisticated model that implements the " + "StyledDocument interface. StyledDocument is a sub-interface of " + "Document that contains methods for manipulating attributes that " + "control the way in which the text in the document is displayed. " + "The Swing text package contains a concrete implementation of " + "StyledDocument called DefaultStyledDocument that is used as the " + "default model for JTextPane and is also the base class from which " + "more specific models, such as the HTMLDocument class that handles " + "input in HTML format, can be created. In order to make use of " + "DefaultStyledDocument and JTextPane, you need to understand how " + "Swing represents and uses attributes.\n"; }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java2D Line Styles Java Tip java.awt 0 06-23-2008 12:05 AM
[SOLVED] hyperlinks in JTextPane olbion New To Java 4 05-05-2008 04:28 AM
JOptionPane - message styles Java Tip Java Tips 0 12-17-2007 10:20 AM
how to insert tables into JTextPane osval AWT / Swing 1 07-29-2007 10:11 AM
Transparent JTextPane Ada AWT / Swing 1 05-31-2007 10:50 PM


All times are GMT +3. The time now is 07:39 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org