×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Nuno Leal
Added: Feb 13, 2021 10:27 AM
Views: 4748
Tags: no tags
  1. codeDisplayArea.getDocument().addDocumentListener(new DocumentListener() {
  2.             public String getText() {
  3.                 int caretPosition = codeDisplayArea.getDocument().getLength();
  4.                 Element root = codeDisplayArea.getDocument().getDefaultRootElement();
  5.                 String text = "1 " + System.getProperty("line.separator");
  6.                 for (int i = 2; i < root.getElementIndex(caretPosition) + 2; i++) {
  7.                     text += " " + i + System.getProperty("line.separator");
  8.                 }
  9.                 return text;
  10.             }
  11.  
  12.             @Override
  13.             public void changedUpdate(DocumentEvent de) {
  14.                 lines.setText(" " + getText() + " ");
  15.             }
  16.  
  17.             @Override
  18.             public void insertUpdate(DocumentEvent de) {
  19.                 lines.setText(" " + getText() + " ");
  20.             }
  21.  
  22.             @Override
  23.             public void removeUpdate(DocumentEvent de) {
  24.                 lines.setText(" " + getText() + " ");
  25.             }
  26.  
  27.         });
  28.         codeDisplayAreaPane.getViewport().add(codeDisplayArea);
  29.         codeDisplayAreaPane.setRowHeaderView(lines);