The Artima Developer Community
Sponsored Link

Java Answers Forum
How to display text in different colors in JTextField of Swings

5 replies on 1 page. Most recent reply: Jul 14, 2005 5:47 PM by Antonio

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Tirupati Reddy

Posts: 1
Nickname: manyam537
Registered: Jul, 2005

How to display text in different colors in JTextField of Swings Posted: Jul 12, 2005 4:37 AM
Reply to this message Reply
Advertisement
I need to display the text in text field in different colors. When my cursor is within the flower brackets i have to enter the string so that it will come in some color, say blue. When my cursor is out of the flower brackets It will display those text in different color, say red.
So can u please give me an idea to do that. Iam using Swings to design the GUI. Urgent need.Waiting for the reply.


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: How to display text in different colors in JTextField of Swings Posted: Jul 12, 2005 6:35 AM
Reply to this message Reply
Though I have no clue what flower brackets are, here's a suggestion:

Try to define your own font with new Attributes.

take a TextAttribute for which you define a foreground.

classes that you'll probably need and should look up on the java api
would be Font, Color, TextAttributes (and its Parent class).

I must say that I'm not sure if this would work 100%, but its
a suggestion.

You probably do need to create your own font though.

After which you'll simply say:

textField.setFont(myNewFont);

Good luck

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: How to display text in different colors in JTextField of Swings Posted: Jul 12, 2005 8:57 AM
Reply to this message Reply
My baad...

Easier way, just used it myself:


myComponent.setForeground(Color.TEXT_COLOR_I_WANT);
// in your case everytime you want to change
// Color you'll be doing the following
textField.setForeground(Color.SOME_COLOR);

Antonio

Posts: 33
Nickname: arhak
Registered: Jul, 2005

Re: How to display text in different colors in JTextField of Swings Posted: Jul 12, 2005 3:35 PM
Reply to this message Reply
I think you better forget about the JTextField and start thinking in JEditorPane and JTextPane. They support differents fonts. A JTextField will let you change the color but for all the text within.

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JEditorPane.html
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTextPane.html

Once you have a JEditorPane or JTextPane (now I don't remember them to suggest wich one) get the document (getDocument) or better set your own Document, an heir of StyledDocument and try the insertString method and the setCharacterAttributes.

You wil have to read the classes:
AttributeSet http://java.sun.com/j2se/1.4.2/docs/api/javax/print/attribute/AttributeSet.html
SimpleAttributeSet http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/SimpleAttributeSet.html
MutableAttributeSet http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/MutableAttributeSet.html
StyleConstants http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/StyleConstants.html
StyledDocument http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/StyledDocument.html

There is a very good java tutorial including the Swing but I don't have it around. Try visiting this two sites:
http://members.tripod.com/Juan_Antonio/descarga/tutorswing.zip
http://members.es.tripod.de/Ozito/descarga/tutorswing.zip
But I'm almost sure those are in spanish.

barron

Posts: 13
Nickname: toniblair
Registered: May, 2005

Re: How to display text in different colors in JTextField of Swings Posted: Jul 14, 2005 1:53 PM
Reply to this message Reply
You could use JEditorPane as previously mentioned. JEditorPane can interpret html. So, you could insert<font color=\"color\"> tags to format the text as you like. JEditorPane I think is the way to go as it gives you all the flexibility html has to offer.

Antonio

Posts: 33
Nickname: arhak
Registered: Jul, 2005

Re: How to display text in different colors in JTextField of Swings Posted: Jul 14, 2005 5:47 PM
Reply to this message Reply
I don't like the html idea. It would overload de JEditorPane and make it slow down. I think the idea is the StyledDocument with the insertString and setCharacterAttributes methods. You may consult also the AttributeSet, SimpleAttributeSet, MutableAttributeSet & StyleConstants classes.

Flat View: This topic has 5 replies on 1 page
Topic: JVM memory allocation Previous Topic   Next Topic Topic: Playing & recording wave files using JTAPI

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use