How do you trap a newline/Enter character in a JTextArea?

Sandip Chitale

There are multiple ways -

  • 
    jtextArea.getDocument().addDocumentListener(
      new DocumentListener() {
        public void insertUpdate(DocumentEvent e) {
          // watch for newline/enter here
        }
        public void removeUpdate(DocumentEvent e) {
        }
        public void changedUpdate(DocumentEvent e) {
          // watch for newline/enter here
        }
    
      }
    }
    
  • Add a KeyListener and look for VK_ENTER key typed event.
  • 
    jtextArea.registerKeyboardAction(
      KeyStroke.getKeyStroke(VK_ENTER, 0, false),
      new ActionListener() {...}
    );
    
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact