Solved

Test area with color

Posted on 2004-04-01
43
270 Views
Last Modified: 2010-03-31
I need a JTextArea or other Component just to add text into.

Something like
public void JTextArea.append(String, Color) ...

Would be perfect

Thanks
0
Comment
Question by:oleber
  • 14
  • 13
  • 10
  • +2
43 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10730667
Don't understand your Q properly. You can use JTextArea, right?

Oh, or do you want that color should be appended in different colours in the same JTextArea? I guess it won't allow that. JTextArea inherits the setForeground () method, which will set the fore-ground colour for all the text, not for a particular text.

You can try using a TextPane or a JEditorPane.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10730682
You can use HTML to set fonts and colours!

;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10730685
With a JLabel you can introduce color via HTML:


JLabel label = new JLabel();
label.setText("<HTML><color="green">This is green text");

If you want to append, make your own MyLabel extends JLabel


class MyLabel extends JLabel {

     public MyLabel() {
         super();
     }
     public MyLabel(String text) {
         super(text);
     }

     public void append(String textToAppend, Color color) {
         String text = getText();
         text += "<BR><color=\"";
       
         // get the color code in hex out of Color and append it to the text variable

         text += "\" + textToAppend;
         setText(text);
     }

}
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 24

Expert Comment

by:sciuriware
ID: 10730691
In other words:

most components, like JButton, JLabel, JTextArea ....   accept HTML texts, even the TipTools for those components
do it.

;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10730692
>> You can use HTML to set fonts and colours!

Can you do that in JTextArea? I don't think so. You can do it in JEditorPane.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10730702
Right, and JEditorPane even accepts .rtf (Rich Text).

;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10730707
public void append(String textToAppend, Color color) {
         String text = getText();
         text += "<BR><color=\"#";
         text += Integer.toString(color.getRed(), 16);
         text += Integer.toString(color.getGreen(), 16);
         text += Integer.toString(color.getBlue(), 16);
         text += "\"> + textToAppend;
         setText(text);
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10730718
In the append() function I added a <BR> to start another line.
Up to you to decide if you want this.
Or if you incorporate that in the text you pass to append().
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10730741
zzynx, I tried your code. It displayed:

<BR><color="#00ff">Hi<BR><color="#ff00">How're you doing in

the JTextArea, when I called it like: t.append ( "Hi", Color.blue ) ; t.append ( "How're you doing", Color.red ) ;
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10730747
Add   <HTML>  in front!!!!

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10730764
The correct string should be                    "<HTML><COLOR=\"#FF0000\">Your text"


;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10730772
1) I was talking about a JLabel
2) Add <HTML> in front:

public void append(String textToAppend, Color color) {
         String text = getText();
         if (text.isEmpty())
            text = "<HTML>";
         text += "<BR><color=\"#";
         text += Integer.toString(color.getRed(), 16);
         text += Integer.toString(color.getGreen(), 16);
         text += Integer.toString(color.getBlue(), 16);
         text += "\"> + textToAppend;
         setText(text);
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10730776
Well, obviously, I had added "<HTML>" at the start.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10730779
>> I was talking about a JLabel

Ah, that solves it. I was trying with JTextArea. Ok, matter resolved.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10730798
And .... zzynx, your code doesn't give 6 hexadecimal digits!

;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10730906
>> zzynx, your code doesn't give 6 hexadecimal digits!
Indeed, some changes to assure leading zeroes are needed.
But hey, the idea is clear, isn't it?

Even some other small changes are needed:

        public void append(String textToAppend, Color color) {
         String text = getText();
         if (text.length()==0)
            text = "<HTML>";
         else
            text += "<BR>";
         text += "<color=\"#";

         // The (better) color stuff, sciuriware will certainly provide  ;)
         // ....

         text += "\">" + textToAppend;
         setText(text);
        }    
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10730972
JFrame mainFrame = new JFrame("Title");
Container c = mainFrame.getContentPane();
JLabel label =  new JLabel("<HTML><STRONG STYLE=\"color:red\">HELLO!</STYLE></STRONG>");
c.add(label, BorderLayout.CENTER);


Works fine; if it does for 'oleber' we can make it more complex .....

;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10731080
Hi sciuriware,

What does that <STRONG STYLE=...> do?
I experience it is needed. But can you tell me why?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10731122
... another kind of BOLD.

We are helping someone to get something working in principle.
The rest he/she should learn from a basical HTML course (or post in the related Forum).

;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10731174
Yes, getting back to the discussion of JTextArea ;-) you still need a JEditorPane or something. You cannot edit a JLabel.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10731217
>> You cannot edit a JLabel.
You certainly can. I just did it.

This is the working append function for MyLabel:

public void append(String textToAppend, Color color) {
         String text = getText();
         if (text.length()==0)
            text = "<HTML>";
         else
            text += "<BR>";
         text += "<font color=\"#";
         String tmp = Integer.toString(color.getRed(), 16);
         if (tmp.length()==1) text += "0";
         text += tmp;
         tmp = Integer.toString(color.getGreen(), 16);
         if (tmp.length()==1) text += "0";
         text += tmp;
         tmp = Integer.toString(color.getBlue(), 16);
         if (tmp.length()==1) text += "0";
         text += tmp;
         text += "\">" + textToAppend;
         setText(text);
}    

You use it like:

        MyLabel label = new MyLabel();
        label.append("Hello", new Color(150,254,244) );
        label.append("world!", new Color(220,120,220) );
0
 
LVL 10

Author Comment

by:oleber
ID: 10731236
The HTML is not a problem.
Editing is not necessary.
But selecting to do a copy is necessary so JLabel is not a good component.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10731239
You are not editing it. You are just changing the text inside it by the setText () method. What I meant was - editing by typing the text from the keyboard. Perhaps the questioner has such requirements, which is why he asked about JTextArea.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10731241
>> You are not editing it. You are just changing the text inside it by the setText () method

That was for zzynx.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10731244
>> But selecting to do a copy is necessary

Can you elaborate more on that?
0
 
LVL 10

Author Comment

by:oleber
ID: 10731300
I need to do copy & past of some text like carecters.

The aplication is calling some scripts and I need to see the STDOUT in black and the STDERR in red.

For instance some file paths will appear there. I will use them in the shell for debugging errors.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 50 total points
ID: 10731391
If you will not be typing anything into it from the keyboard, I don't see why you don't want to use a JLabel.

Anyway, you can use a disabled JEditorPane.

JEditorPane jepMessage = new JEditorPane () ;
JPanel jpEditor = new JPanel ( new BorderLayout () ) ;
jepMessage.setContentType ( "text/html" ) ;
jepMessage.setText ( "<HTML><BODY>" + your_HTML_text + "</BODY></HTML>" ) ;
jepMessage.setEnabled ( false ) ;
JScrollPane jspMsg = new JScrollPane ( jepMessage ) ;
jspMsg.setPreferredSize ( new Dimension ( 500, 300 ) ) ;
jpEditor.add ( jspMsg ) ;

Whenever you set the text, use:

String sCurrentText = jepMessage.getText () ;
jepMessage.setText ( .substring ( 0, sCurrentText.lastIndexOf ( "</body>" ) ) + your_new_HTML_text + "</BODY></HTML>" ) ;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10731419
And with a little modifications you can use the stuff I have at the append() function
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 total points
ID: 10731431
All right:

    public class MyArea extends JEditorPane {
       
        public MyArea() {
            super();
            setEditorKit( new javax.swing.text.html.HTMLEditorKit() );
        }
       
        public void append(String textToAppend, Color color) {
         String text = getText();
         if (text.length()==0)
            text = "<HTML><body></body></html>";
         else
            text = "<BR>";
         text += "<font color=\"#";
         String tmp = Integer.toString(color.getRed(), 16);
         if (tmp.length()==1) text += "0";
         text += tmp;
         tmp = Integer.toString(color.getGreen(), 16);
         if (tmp.length()==1) text += "0";
         text += tmp;
         tmp = Integer.toString(color.getBlue(), 16);
         if (tmp.length()==1) text += "0";
         text += tmp;
         text += "\">" + textToAppend;
         setText( getText().substring (0, getText().lastIndexOf("</body>" ) ) + text + "</body></html>" );
        }    

    }

Usage:

        MyArea area = new MyArea();
        area.append("Hello", new Color(150,254,244) );
        area.append("world!", new Color(220,120,220) );
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10732106
>> your_new_HTML_text

- in my code, would have all color, font, etc tags, like:

"<FONT COLOR =\"#" + Integer.toString ( color.getRed (), 16 ) + "\"> Hi </FONT>"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10736722
String sColor = "#" + Integer.toHexString(color.hashCode() & 0xFFFFFF);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10808035
hi oleber,
what about closing this question by accepting one or more of the comments?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10977979
As you can see, I already asked the author to close this Q by accepting one or more comments.
I think I gave some valuable input to solve the author's problem.
That's why I expect one of my comments to be accepted as assisted answer.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10978677
Agreed,
;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10983572
Recommendation: Split zzynx, sciuriware, mayankeagle, CEHJ.
0
 
LVL 10

Author Comment

by:oleber
ID: 10983857
Sorry I had the ideia this was losed and I has outside for some time.
I'm thinking to split the points by 'zzynx' and 'mayankeagle' But I don't know how to do that?

Can someone help me
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10983995
When you click accept, there is an option "Split points".
And 150 can be divided several ways ....

;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10984174
>> I'm thinking to split the points by 'zzynx' and 'mayankeagle' But I don't know how to do that?
See: http://www.experts-exchange.com/help.jsp#hi69
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10984486
The Split Points link should be just above the Comment Box. Can you not see it?
0
 
LVL 10

Author Comment

by:oleber
ID: 10988468
sorry for making you all wait.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10993900
Thanks for accepting
(at last ;°)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10994320
;-)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now