How to underline a string in Java?

B

Bruce Sam

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?
 
B

Boudewijn Dijkstra

Bruce Sam said:
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.
 
T

Thomas Kellerer

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
 
P

Paul van Rossem

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.
 
V

VisionSet

Bruce Sam said:
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.
 
Y

Yamin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top