slee
slee

Reputation: 491

How can I produce lines that aligned by words in java swing textArea?

lines like this:

 id    title         price    quantity    sum

 001   c++ primer    20.0     5           100.0

I want the two lines look like the above .The first line is the head.

And lines below are information about books. I try to use blanks to achice it,

but it's not that good.can you help?thank you.

Upvotes: 1

Views: 964

Answers (4)

camickr
camickr

Reputation: 324118

The better choice is to use a JTable. Read the section from the Swing tutorial on How to Use Tables for more information and working examples.

If you must use a JTextArea, then you need to use a monospaced font.

textArea.setFont( new Font("monospaced", Font.PLAIN, 10) );

or any other font that that has a fixed size for each character.

Upvotes: 1

Costis Aivalis
Costis Aivalis

Reputation: 13728

I would use a JEditorPane here with "text/html" content type and make an HTML table.

Upvotes: 0

Mot
Mot

Reputation: 29550

Of course, you have to use a fixed-width-font. But detecting one which works for all characters is not trivial (or impossible?), because, e.g., Asian characters usually are two latin characters wide.

Upvotes: 0

moinudin
moinudin

Reputation: 138357

Use a Formatter object.

Upvotes: 2

Related Questions