JTextComponent: getDocument() : JTextComponent « javax.swing « Java by API






JTextComponent: getDocument()

  

 

import javax.swing.JTextPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTextComponent textcomp = new JTextPane();
    textcomp.setText("Initial Text");
    textcomp.getDocument().addDocumentListener(new DocumentListener() {
      public void insertUpdate(DocumentEvent evt) {
        int off = evt.getOffset();
        System.out.println("off:"+off);
        int len = evt.getLength();
        System.out.println("len:"+len);
        try {
          String str = evt.getDocument().getText(off, len);
          System.out.println(str);
        } catch (BadLocationException e) {
        }
      }
      public void removeUpdate(DocumentEvent evt) {
        int off = evt.getOffset();
        System.out.println("off:"+off);
        int len = evt.getLength();
        System.out.println("len:"+len);
      }
      public void changedUpdate(DocumentEvent evt) {
        int off = evt.getOffset();
        System.out.println("off:"+off);
        int len = evt.getLength();
        System.out.println("len:"+len);
      }
    });
  }
}

   
    
  








Related examples in the same category

1.JTextComponent.KeyBinding
2.JTextComponent: addCaretListener(CaretListener listener)
3.JTextComponent: getActions()
4.JTextComponent: getCaretPosition()
5.JTextComponent: getHighlighter()
6.JTextComponent: getSelectedText()
7.JTextComponent: getText(int offs, int len)
8.JTextComponent: moveCaretPosition(int pos)
9.JTextComponent: print(MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintService service, PrintRequestAttributeSet attributes, boolean interactive)
10.JTextComponent: read(Reader in, Object desc)
11.JTextComponent: replaceSelection(String content)
12.JTextComponent: select(int selectionStart, int selectionEnd)
13.JTextComponent: setDocument(Document doc)
14.JTextComponent: setDragEnabled(boolean b)
15.JTextComponent: setFocusAccelerator(char aKey)
16.JTextComponent: setNavigationFilter(NavigationFilter filter)
17.JTextComponent: setSelectedTextColor(Color c)
18.JTextComponent: setSelectionColor(Color c)
19.JTextComponent: setSelectionEnd(int selectionEnd)
20.JTextComponent: setSelectionStart(int selectionStart)
21.JTextComponent: write(Writer out)