Link to home
Start Free TrialLog in
Avatar of oleber
oleberFlag for Portugal

asked on

Test area with color

I need a JTextArea or other Component just to add text into.

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

Would be perfect

Thanks
Avatar of Mayank S
Mayank S
Flag of India image

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.
Avatar of sciuriware
sciuriware

You can use HTML to set fonts and colours!

;JOOP!
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);
     }

}
In other words:

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

;JOOP!
>> 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.
Right, and JEditorPane even accepts .rtf (Rich Text).

;JOOP!
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);
}
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().
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 ) ;
Add   <HTML>  in front!!!!

;JOOP!
The correct string should be                    "<HTML><COLOR=\"#FF0000\">Your text"


;JOOP!
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);
}
Well, obviously, I had added "<HTML>" at the start.
>> I was talking about a JLabel

Ah, that solves it. I was trying with JTextArea. Ok, matter resolved.
And .... zzynx, your code doesn't give 6 hexadecimal digits!

;JOOP!
>> 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);
        }    
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!
Hi sciuriware,

What does that <STRONG STYLE=...> do?
I experience it is needed. But can you tell me why?
... 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!
Yes, getting back to the discussion of JTextArea ;-) you still need a JEditorPane or something. You cannot edit a JLabel.
>> 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) );
Avatar of oleber

ASKER

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.
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.
>> You are not editing it. You are just changing the text inside it by the setText () method

That was for zzynx.
>> But selecting to do a copy is necessary

Can you elaborate more on that?
Avatar of oleber

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And with a little modifications you can use the stuff I have at the append() function
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> your_new_HTML_text

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

"<FONT COLOR =\"#" + Integer.toString ( color.getRed (), 16 ) + "\"> Hi </FONT>"
String sColor = "#" + Integer.toHexString(color.hashCode() & 0xFFFFFF);
hi oleber,
what about closing this question by accepting one or more of the comments?
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.
Agreed,
;JOOP!
Recommendation: Split zzynx, sciuriware, mayankeagle, CEHJ.
Avatar of oleber

ASKER

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
When you click accept, there is an option "Split points".
And 150 can be divided several ways ....

;JOOP!
>> I'm thinking to split the points by 'zzynx' and 'mayankeagle' But I don't know how to do that?
See: https://www.experts-exchange.com/help.jsp#hi69
The Split Points link should be just above the Comment Box. Can you not see it?
Avatar of oleber

ASKER

sorry for making you all wait.
Thanks for accepting
(at last ;°)