oleber
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
Something like
public void JTextArea.append(String, Color) ...
Would be perfect
Thanks
You can use HTML to set fonts and colours!
;JOOP!
;JOOP!
With a JLabel you can introduce color via HTML:
JLabel label = new JLabel();
label.setText("<HTML><colo r="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);
}
}
JLabel label = new JLabel();
label.setText("<HTML><colo
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!
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.
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!
;JOOP!
public void append(String textToAppend, Color color) {
String text = getText();
text += "<BR><color=\"#";
text += Integer.toString(color.get Red(), 16);
text += Integer.toString(color.get Green(), 16);
text += Integer.toString(color.get Blue(), 16);
text += "\"> + textToAppend;
setText(text);
}
String text = getText();
text += "<BR><color=\"#";
text += Integer.toString(color.get
text += Integer.toString(color.get
text += Integer.toString(color.get
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().
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="#ff 00">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 ) ;
<BR><color="#00ff">Hi<BR><
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!
;JOOP!
The correct string should be "<HTML><COLOR=\"#FF0000\"> Your text"
;JOOP!
;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.get Red(), 16);
text += Integer.toString(color.get Green(), 16);
text += Integer.toString(color.get Blue(), 16);
text += "\"> + textToAppend;
setText(text);
}
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.get
text += Integer.toString(color.get
text += Integer.toString(color.get
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.
Ah, that solves it. I was trying with JTextArea. Ok, matter resolved.
And .... zzynx, your code doesn't give 6 hexadecimal digits!
;JOOP!
;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);
}
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!
Container c = mainFrame.getContentPane()
JLabel label = new JLabel("<HTML><STRONG STYLE=\"color:red\">HELLO!
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?
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!
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.get Red(), 16);
if (tmp.length()==1) text += "0";
text += tmp;
tmp = Integer.toString(color.get Green(), 16);
if (tmp.length()==1) text += "0";
text += tmp;
tmp = Integer.toString(color.get Blue(), 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) );
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.get
if (tmp.length()==1) text += "0";
text += tmp;
tmp = Integer.toString(color.get
if (tmp.length()==1) text += "0";
text += tmp;
tmp = Integer.toString(color.get
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) );
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.
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.
That was for zzynx.
>> But selecting to do a copy is necessary
Can you elaborate more on that?
Can you elaborate more on that?
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And with a little modifications you can use the stuff I have at the append() function
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> your_new_HTML_text
- in my code, would have all color, font, etc tags, like:
"<FONT COLOR =\"#" + Integer.toString ( color.getRed (), 16 ) + "\"> Hi </FONT>"
- 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?
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.
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!
;JOOP!
Recommendation: Split zzynx, sciuriware, mayankeagle, CEHJ.
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
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!
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
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?
ASKER
sorry for making you all wait.
Thanks for accepting
(at last ;°)
(at last ;°)
;-)
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.