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 08-09-2007, 09:26 PM
Member
 
Join Date: Aug 2007
Posts: 3
jkhoa is on a distinguished road
Preventing inserted text from becoming colored from previous style
I have a jtextpane I have alreadly colored in black, and red. Red is for the text inside the brackets. This seems to work, but the problem is when I insert text imediately next to a red bracket, the next text that comes out is red as well, while it should be black. Right now what I'm doing to fix the problem is coloring everything black first then color in the red, but that makes the jtextpane blinks between two colors. I was wondering if there is some simpler way to prevent the style from being "contagious".

thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-10-2007, 01:31 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
This seems to work okay.
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class StyleIntegrity { JTextPane textPane; public StyleIntegrity() { System.setProperty("swing.aatext", "true"); String text = "This component models paragraphs that are composed of " + "runs of character level attributes. Each paragraph may have a " + "logical style attached to it which contains the default attributes " + "to use if not overridden by attributes set on the paragraph or " + "character run."; textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); createStyles(doc); setContent(doc, text); styleContent(doc); } private void createStyles(StyledDocument doc) { Style baseStyle = doc.addStyle("base", null); StyleConstants.setFontFamily(baseStyle, "Lucida Sans Unicode"); StyleConstants.setFontSize(baseStyle, 18); StyleConstants.setFirstLineIndent(baseStyle, 20f); StyleConstants.setLeftIndent(baseStyle, 10f); Style style = doc.addStyle("red", baseStyle); StyleConstants.setForeground(style, Color.red); } private void setContent(StyledDocument doc, String text) { try { doc.insertString(0, text, doc.getStyle("base")); } catch(BadLocationException e) { System.out.printf("Bad location error: %s%n", e.getMessage()); } } private void styleContent(StyledDocument doc) { Style style = doc.getStyle("base"); doc.setLogicalStyle(0, style); style = doc.getStyle("red"); doc.setCharacterAttributes(116, 66, style, false); } private JMenuBar getMenuBar() { JMenu edit = new JMenu("Edit"); edit.add(new DefaultEditorKit.CutAction()); edit.add(new DefaultEditorKit.CopyAction()); edit.add(new DefaultEditorKit.PasteAction()); edit.getItem(0).setText("cut"); edit.getItem(1).setText("copy"); edit.getItem(2).setText("paste"); JMenuBar menuBar = new JMenuBar(); menuBar.add(edit); return menuBar; } private JScrollPane getContent() { return new JScrollPane(textPane); } public static void main(String[] args) { StyleIntegrity test = new StyleIntegrity(); JFrame f = new JFrame(); f.setJMenuBar(test.getMenuBar()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test.getContent()); f.setSize(500,400); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-10-2007, 01:36 AM
Member
 
Join Date: Aug 2007
Posts: 3
jkhoa is on a distinguished road
Thanks alot
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
NetBeans-style Dockable-windows and toolbar? jimm1 Advanced Java 0 01-31-2008 09:41 PM
Using SWT Text Java Tip Java Tips 0 01-10-2008 11:41 AM
how to set indentation to a text elizabeth New To Java 1 08-06-2007 07:42 PM
Using previous with CardLayout uncopywritable New To Java 2 08-05-2007 10:43 PM
map javax.swing.text.Element to javax.swing.text.View elizabeth New To Java 1 07-30-2007 08:02 PM


All times are GMT +3. The time now is 03:25 AM.


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