Solved

text area and  string

Posted on 2002-04-30
9
325 Views
Last Modified: 2006-11-17
Sir,
   I have to display the string "hello world" in a single text area in a frame (or applet) . String "hello" should be displayed in (say) blue
color and "world " in (say) red color.how can i proceed?
thanks
paramjit
ver -- JDK 1.3
0
Comment
Question by:paramibm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 6980442
Homework? We're here to help people who have *already* produced something of their own.
0
 
LVL 35

Expert Comment

by:girionis
ID: 6980477
 Hehehehe, CEHJ could not be more right.
0
 

Author Comment

by:paramibm
ID: 6980556
sir
i had done a project in java to create Multithreaded Chat server .
 thanks
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 18

Expert Comment

by:bobbit31
ID: 6981099
use a JTextArea (in this example, named jta) and then do something like:

jta.select(0, 5);
jta.setSelectedTextColor(Color.red);
jta.select(5, 10);
jta.setSelectedTextColor(Color.blue);
0
 

Expert Comment

by:sunilvirus
ID: 6983964
i am also having the same problem(slightly different).
see my question "Displaying colors in textfield"

one useful link given in my following is
http://developer.java.sun.com/developer/onlineTraining/GUI/Swing2/Magercises/M3/index.html

i think it will serve your purpose.
0
 

Expert Comment

by:sunilvirus
ID: 6988072
hello bobbit31

your code in not working as required.
when using the method setSelectedTextColor() for the second time,
the color applied in the first call is not being retained.
it is again changing back to the foreground color.

this is because the previous selection is lost when the new text is selected.
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 6989097
you could use a JTextPane to have formatted text... in this case, I set the content type to "text/html":

public class ColorText extends JApplet {
   
    private JTextPane jtp;
       
    public void init()
        {
           jtp = new JTextPane();
           jtp.setContentType("text/html");
           getContentPane().add(jtp);

           jtp.setText("<font color=#0000FF>This is <b>Blue</b></font> <font color=#FF0000>This is <b>Red</b></font>");
     }
}
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7992747
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: bobbit31
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8048333
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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