Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem whith newline in JTextPane  RSS feed

 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have a keyListener that checks if the user presses the return key



this works fine. If the if statement returns true I check to see if I'm in a <ul><li></li></ul> tag.
if the user presses return when in a <li></li> it should append a new <li></li>

I do this by adding the following code


this code works but not as expected. I get a new line between the <li> elements and I want to remove this \n line. How can I do this?
 
todd runstein
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Total guess - have you tried consuming the KeyEvent?

if( ke.getKeyChar() == KeyEvent.VK_ENTER ){
if( inElement( HTML.Tag.LI ) ){
..... code .....
ke.consume();
}
}

Worth a shot!
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by todd runstein:
Total guess - have you tried consuming the KeyEvent?

if( ke.getKeyChar() == KeyEvent.VK_ENTER ){
if( inElement( HTML.Tag.LI ) ){
..... code .....
ke.consume();
}
}

Worth a shot!


That would be it I'm sure. The JTextPane receives the event still and inserts a new line. Consuming the event will put a stop to that.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but that didn't help. It still is a line between the <li></li> and the next </li></li>

looks like this if we view the HTML source



If i go intor the source and remove the \n line then the code will produce 2 dots in the WISWYG editor( when viewing the text in the JTextPane without seeing the HTML tags ) every time i hit the return but when I view the html source it's just 1 <li></li> ??

here is the javacode that handles the


and here is the check method



really hope you can help me
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some testing I put the code in the keyPressed method instead of the keyTyped method and the consume() method worked?

Is this the right way to do it or is there a muh better solution?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!