Solved

Test area with color

Posted on 2004-04-01
43
251 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:mayankeagle
Comment Utility
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
Comment Utility
You can use HTML to set fonts and colours!

;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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:mayankeagle
Comment Utility
>> 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
Comment Utility
Right, and JEditorPane even accepts .rtf (Rich Text).

;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
Add   <HTML>  in front!!!!

;JOOP!
0
 
LVL 24

Expert Comment

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


;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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:mayankeagle
Comment Utility
Well, obviously, I had added "<HTML>" at the start.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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
Comment Utility
And .... zzynx, your code doesn't give 6 hexadecimal digits!

;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
... 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:mayankeagle
Comment Utility
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
Comment Utility
>> 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Author Comment

by:oleber
Comment Utility
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:mayankeagle
Comment Utility
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:mayankeagle
Comment Utility
>> 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:mayankeagle
Comment Utility
>> But selecting to do a copy is necessary

Can you elaborate more on that?
0
 
LVL 10

Author Comment

by:oleber
Comment Utility
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:
mayankeagle earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
>> 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
Comment Utility
String sColor = "#" + Integer.toHexString(color.hashCode() & 0xFFFFFF);
0
 
LVL 37

Expert Comment

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

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility
Agreed,
;JOOP!
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Recommendation: Split zzynx, sciuriware, mayankeagle, CEHJ.
0
 
LVL 10

Author Comment

by:oleber
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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:mayankeagle
Comment Utility
The Split Points link should be just above the Comment Box. Can you not see it?
0
 
LVL 10

Author Comment

by:oleber
Comment Utility
sorry for making you all wait.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanks for accepting
(at last ;°)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
;-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

762 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

13 Experts available now in Live!

Get 1:1 Help Now