Forum Stats

  • 3,783,594 Users
  • 2,254,807 Discussions
  • 7,880,474 Comments

Discussions

get a line in JTextArea

843807
843807 Member Posts: 46,582
edited Feb 13, 2003 4:35AM in Java Desktop Applications
Hi. I don't kwow how to get the line over the actual cursor position in a JTextArea.
Thank you for suggestions.

Comments

  • 843807
    843807 Member Posts: 46,582
    What was that agian ?Icouldn't get it .
  • 843807
    843807 Member Posts: 46,582
    You want to get the text from the JTextArea?
    //Create your text area.
    JTextArea myTextArea = new JTextArea(...);
    
    //Later on, get the contents.
    try {
        //get the text from position Zero to the end of the text.
        String text = myTextArea.getDocument().getText(0,
            myTextArea.getDocument().getLength());
    } catch (Exception e){
        //You must catch or throw the BadLocationException,
        //but you shouldn't get one.
    }
    //The contents of the JTextArea are now in the String text.
    Hope this helps. you'll have the entire text area as a string.
  • 843807
    843807 Member Posts: 46,582
    The
    getLineOfOffset(int line) 
    method will give you the line at the given offset, where the parameter must be the caret position

    i.e
       int iCaretPosition   = txtAreaObj.getCaretPosition();
       int iLineNumber = txtAreaObj.getLineOfOffset(iCaretPosition);
    Hope that helps...
This discussion has been closed.