Is there a way to catch non-displayable characters before they are written to JTextArea?

I change the default keyTyped behavior for char output in JTextArea, however, when I copy and paste, it displays a non-displayable character.

I need this to work on multiple operating systems, so using case 22: does not work, as it does not apply to Mac. Does the sun either have a case for catching pastes / copies without OS restrictions, or does someone know a good job?

+3
source share
2 answers

You might want to examine document filters .

( ) , JTextArea. , AbstractDocument . - :

AbstractDocument doc = (AbstractDocument)textArea.getDocument(); 
doc.setDocumentFilter( new DocumentFilter() {
  public void insertString( FilterBypass fb, int offset, String string,
      AttributeSet attr ) throws BadLocationException
  {
    // Test string here and modify if required, then call super.insertString()
    // (usually called on a "paste")
  }

  public void replace( FilterBypass fb, int offset, int length,
      String text, AttributeSet attrs ) throws BadLocationException
  {
    // Test string here and modify if required, then call super.replace()
    // (usually called when characters are typed)
  }
});

( "" , AbstractDocument , , ).

+2

Source: https://habr.com/ru/post/1729888/


All Articles