Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: How to underline a string in Java?

2,959 views
Skip to first unread message

Boudewijn Dijkstra

unread,
Dec 15, 2004, 4:13:25 AM12/15/04
to
"Bruce Sam" <perse...@yahoo.com.cn> schreef in bericht
news:1103080126.1...@z14g2000cwz.googlegroups.com...
>I thinked direct draw a line is not a good way.Also,I don't need the
> method of using HTML to implement this.Are there a good method to
> underline a string in java?

If the API would support underlining, then directly drawing a line isn't a
good way. But it isn't supported.

Use java.awt.Font#getBaselineFor(char) to position your line correctly.


Thomas Kellerer

unread,
Dec 15, 2004, 4:29:00 AM12/15/04
to
On 15.12.2004 04:08 Bruce Sam wrote:

> I thinked direct draw a line is not a good way.Also,I don't need the
> method of using HTML to implement this.Are there a good method to
> underline a string in java?
>

Where do you display the String? If you use a JLabel you can do the following:

JLabel l = new JLabel("<html><u>This is underlined</u> and this is not</html>");

Thomas

Paul van Rossem

unread,
Dec 15, 2004, 5:40:31 AM12/15/04
to
On 15-12-2004 04:08, Bruce Sam wrote:
> I thinked direct draw a line is not a good way.Also,I don't need the
> method of using HTML to implement this.Are there a good method to
> underline a string in java?
>
Yes there is, see java.awt.font.TextAttribute and
java.text.AttributedString.

Paul.

VisionSet

unread,
Dec 15, 2004, 7:06:18 AM12/15/04
to

"Bruce Sam" <perse...@yahoo.com.cn> wrote in message
news:1103080126.1...@z14g2000cwz.googlegroups.com...


> I thinked direct draw a line is not a good way.Also,I don't need the
> method of using HTML to implement this.Are there a good method to
> underline a string in java?
>

You can probably also do this in unicode using combined diacritical or
whatever they are called. See unicode.org and use there codes like \uxxxx
where xxxx is the hex of there code.

--
Mike W


Bruce Sam

unread,
Dec 14, 2004, 10:08:46 PM12/14/04
to

Yamin

unread,
Dec 15, 2004, 1:33:14 PM12/15/04
to
I am curious to know where you want to underline the string? That is
what is the output or display you want to see the underline? A String
on its own does not have any attributes like underline or bold or
anything. All it holds are the characters.

Is the output just a label? Then the method used by Thomas is the
easiest. I've always liked this method. It makes things really easy
to customize the display.

Is the output a file, then you have to find out what the file format
says about underlining if any.

0 new messages