How do I handle styled text elegantly?
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ingo_Gruell
Posted On:   Monday, August 6, 2001 09:27 AM

I want to create a small text editor that allows the user to set fonts, font sizes and font colors (as well as underline text and align paragraphs). Right now I use a JTextPane and lots of ugly code. For example, to change text colors, I open a JColorChooser and after that create a StyledEditorKit.ForegroundAction , trigger its actionPerformed method and forget about it - next color means new action is created. Why isn't there a way to change the color for an existing ForeGroundAction ? And I haven't figured out yet how to set a font. With JTextPane.setFont I can set a font, but that effects all of the text.    More>>

I want to create a small text editor that allows the user to set fonts,
font sizes and font colors (as well as underline text and align paragraphs).



Right now I use a JTextPane and lots of ugly code.

For example, to change text colors, I open a JColorChooser and after that
create a StyledEditorKit.ForegroundAction , trigger its actionPerformed method
and forget about it - next color means new action is created.

Why isn't there a way to change the color for an existing ForeGroundAction ?



And I haven't figured out yet how to set a font.

With JTextPane.setFont I can set a font, but that effects all of the text.
With StyledEditorKit.FontFamilyAction I can only set the font family, not the font itself.



So, what I really want to know is:

Is there a way to handle styled text with Swing components in an elegant way?

I found a Cocoa class called NSTextView
(http://developer.apple.com:80/techpubs/macosx/Cocoa/Reference/ApplicationKit/ObjC_classic/Classes/NSTextView.html)
that sounds like what I want to have (I quote: "An NSTextView maintains
a set of typing attributes (font, size, color, and so on) ... The user can
change the typing attributes by choosing menu commands and using
utilities such as the Font Panel. You can also set the typing attributes
programmatically using setTypingAttributes:, though you should rarely
find need to do so unless creating a subclass.", "By default, an NSTextView
keeps the Font Panel updated with the first font in its selection, or of its
typing attributes" - as a lazy programmer, I really like the idea of not
having to write code ;-)

I don't know if NSTextView really does what its descriptions claims
but it would be very convenient if there are similar Swing components that
I simply haven't found yet.

   <<Less

Re: How do I handle styled text elegantly?

Posted By:   Phil_Wilson  
Posted On:   Wednesday, March 6, 2002 09:27 AM

Hi Ingo, like you I've tried to do exactly the same thing, and eventually arrived at precisely the same conclusion.

You're right, for such a normally elegant OO language like Java, this area seems to have been an oversight. It may be worth remarking on it in the Sun forums over at java.sun.com.

Re: How do I handle styled text elegantly?

Posted By:   Laura_Dietz  
Posted On:   Wednesday, August 22, 2001 06:32 AM

Ingo,


Maybe you should have a look at javax.swing.text.StyleContext.



this class is something like a container for collecting and reusing styles.



If you use AttributeSets, StyleConstants can set attributes like fonts, sizes and bold-or-not.



Regards,
Laura

Re: How do I handle styled text elegantly? --- now have working code

Posted By:   Ingo_Gruell  
Posted On:   Wednesday, August 8, 2001 05:53 AM

OK, now I have some working code that does, what I need and
isn't quite as ugly as the code I began experimenting with.

The following code snippet assumes there is a Font called
myFont and some selected text in myJTextPane:



StyledDocument myDoc = myJTextPane.getStyledDocument();

Style myStyle = myDoc.addStyle(null, null);

StyleConstants.setBold(myStyle, myFont.isBold());

StyleConstants.setItalic(myStyle, myFont.isItalic());

StyleConstants.setFontFamily(s, myFont.getFamily());

StyleConstants.setFontSize(s, myFont.getSize());

int offset = myJTextPane.getSelectionStart();

int length = myJTextPane.getSelectionEnd() - offset;

myDoc.setCharacterAttributes(offset, length, myStyle, false);




Still, for object oriented programming this is rather awkward code.
If Font conformed to the Style interface, one
line of code would be do the job:



myJTextPane.getStyledDocument().setCharacterAttributes(myJTextPane.getSelectionStart(), (myJTextPane.getSelectionEnd() - myJTextPane.getSelectionStart()), myFont, false);



Unfortunately, that is not the case.

Re: How do I handle styled text elegantly?

Posted By:   Christopher_Schultz  
Posted On:   Monday, August 6, 2001 09:36 AM

I've never done anything like this, but perhaps JEditorPane and friends may suit your needs better than the JTextPane. I know that the HTMLEditorPane is a subclass of that, and can render HTML, including colors, fonts, etc. pretty well.



-chris
About | Sitemap | Contact