Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java: Detecting backspace JTextArea

  • 11-02-2004 12:12PM
    #1
    Posts: 431 ✭✭


    Hey all,

    Could anyone tell me how to detect when a backspace key results in the cursor returning to the previous line (thus reducing the number of lines in the area) ?

    I have a subclass of PlainDocument which can detect the '\b' character but how could I find out if I have returned to a previous line?

    Any help on this would be great,
    thanks :)


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    I'm sure there's probably a prettier way of doing it, but set a variable noOfLines to 1 at the start. If you add a KeyListener to the Textarea, and on every key press, do a getLineCount(), then compare it to noOfLines, you can determine if you have gone up or down by one line. Then you change noOfLines to match getLineCount().

    I've never used a JTextArea before, so I'm not sure if it returns the number of actual lines (i.e. the number of String preceded or ended by '\n', regardless of how many rows in the TextArea) or the number of rows being used in the TextArea. Perhaps countRows() may do that.


  • Posts: 431 ✭✭ [Deleted User]


    I'll give that a try, thanks for the reply


Advertisement