JTextField with styled blocks

749 views Asked by At

I need some kind of JTextField that replace carriage return characters by a styled block displaying the text <New Line> with another font, style or color.

By example, the text..

Dear Mr. Smith,\nblablabla...\nRegards...

..would be shown to the user, in a one line JTextField, this way :

Dear Mr. Smith,<New Line>blablabla...<New Line>Regards...

These <New Line> should be blocks that are selectable as if it was a single character and could be deleted with a single backspace. Pressing the return key would add a <New Line> at the caret position.

I think it can be possible by overriding PlainDocument, but I'm stuck in difficult to understand Document and EditorKit APIs. I would need some pointers about this one...

I do not have any objection using a JTextPane or JEditorPane as long as it behaves like a JTextField (one line, no scroll-bars).

Any suggestions?

1

There are 1 answers

7
Andrew Thompson On BEST ANSWER

Any suggestions ?

Use a JTextArea (a multi-line component) instead.

For an example, see this answer:


It would be simpler, but this is not what I have to do...

Fair enough, I missed the part that stated:

..with another font, style or color.

That would require a styled document such as JEditorPane or JTextPane.