Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Paragraphs in a text component - How ?  RSS feed

 
D R Wilkinson
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to be able to display paragraphs in a text component as distinct blocks of text, like in a word processor.

How do I make a text component add space between paragraphs when the user pressing the enter key?

Thank you.
 
Morgan Christian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a key listener and listen for the enter and as the action do a:

myTextArea.append("\n");

http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html


you might try something like this:
 
D R Wilkinson
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Morgan.

That would put a new line in between the two paragraphs but what I want is to not have a newline. I want to have space between the last line of the preceeding paragraph and the first line of the following paragraph, that is larger than the space between the lines within both paragraphs - like within a word processor.

In such a program, you can set the styling of the text to display spaces between paragraphs - this is what I want to achieve but I don't know how.

Again, thank you for your help but it doesn't solve the problem. Now I have explained myself better, maybe you or someone else can help me achieve this.
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't give much background (are you using
a JTextPane? a JTextArea??) but you may want
to take a look at these two methods.

StyleConstants.setSpaceAbove()
StyleConstants.setSpaceBelow()

Those may work for you, depending.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!