underlining a text or a character

hello

I want to underline and make the string "hello" to be. the code is not showing any error but the "hello" string is not changing to RED or UNDERLINED.

can anybody help?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.*;

publicclass TypingTestextends JFrame

{

public TypingTest()

{

super("Typing Test" );

Container cn = getContentPane();

cn.setLayout(null );

JPanel textPanel1, textPanel2;

JTextPane textPane1, textPane2;

//JPanel1

textPanel1 =new JPanel();

textPanel1.setLayout(null );

StringBuffer bf =new StringBuffer("hello" );

textPanel1.setBounds( 0,0,600,300);

textPane1 =new JTextPane();

String s ="";

for(int i = 0; i < bf.length(); i++)

s = s + bf.charAt(i);

Style style = textPane1.addStyle("Red",null);

StyleConstants.setForeground(style, Color.red);

style = textPane1.addStyle("Red Underline", style);

StyleConstants.setUnderline(style,true);

textPane1.setText( s );

textPane1.setBounds( 0,0,600,290);

textPanel1.add( textPane1 );

// Panel2

textPanel2 =new JPanel();

textPanel2.setLayout(null );

textPanel2.setBounds( 0,300,600,300);

textPane2 =new JTextPane();

textPane2.setBounds( 0,0,600,290);

textPanel2.add( textPane2 );

cn.add( textPanel1 );

cn.add( textPanel2 );

setSize( 600, 600 );

setVisible(true );

repaint();

}

publicstaticvoid main( String args[] )

{

TypingTest app =new TypingTest();

app.addWindowListener(

new WindowAdapter(){

publicvoid WindowClosing( WindowEvent we){

System.exit( 0 );

}

}

);

}

}

[3244 byte] By [tomal123a] at [2007-11-27 6:22:40]
# 1

Why did you repost this question? Did you read the answer in your other posting? It said:

a) In the future....

b) And it then provided a link to a working example that does what you want.

So you already have the answer. Quit wasting peoples time by posting a question you already have an answer to.

camickra at 2007-7-12 17:39:50 > top of Java-index,Desktop,Core GUI APIs...
# 2
No. I dont have the answer I desired. You havent seen my code. whatz wrong with it. I want to know that. I also want to know how to change color or underline character by character
tomal123a at 2007-7-12 17:39:50 > top of Java-index,Desktop,Core GUI APIs...
# 3
hi!Read This http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.htmland test [url= http://java.sun.com/docs/books/tutorialJWS/uiswing/components/ex6/TextComponentDemo.jnlp]this[/url]regardsAniruddha
Aniruddha-Herea at 2007-7-12 17:39:50 > top of Java-index,Desktop,Core GUI APIs...
# 4

> whatz wrong with it. I want to know that. I also want to know how to change color or underline character by character

You define the styles with the addStyle method,

but you don't apply those styles.

To apply a style, use getStyledDocument().insertString method.

(Note that you don't store the reference to your first style because you have the same variable for your second style. Either store them both or retreive them with getStyle method.)

tom_jansena at 2007-7-12 17:39:50 > top of Java-index,Desktop,Core GUI APIs...
# 5
> I also want to know how to change color or underline character by character That is exactly what my code does.
camickra at 2007-7-12 17:39:50 > top of Java-index,Desktop,Core GUI APIs...