Example usage for javax.swing.event DocumentEvent getLength

List of usage examples for javax.swing.event DocumentEvent getLength

Introduction

In this page you can find the example usage for javax.swing.event DocumentEvent getLength.

Prototype

public int getLength();

Source Link

Document

Returns the length of the change.

Usage

From source file:Main.java

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) {
            }//from w  ww.  j a  v a 2s  .c o m
        }

        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);
        }
    });
}

From source file:MyDocumentListener.java

public void updateLog(DocumentEvent e, String action) {
    Document doc = (Document) e.getDocument();
    int changeLength = e.getLength();
    System.out.println(changeLength + " character" + ((changeLength == 1) ? " " : "s ") + action + " "
            + doc.getProperty("name") + "." + newline + "  Text length = " + doc.getLength() + newline);
}

From source file:ListenerSample.java

public void printInfo(DocumentEvent documentEvent) {
    System.out.println("Offset: " + documentEvent.getOffset());
    System.out.println("Length: " + documentEvent.getLength());
    DocumentEvent.EventType type = documentEvent.getType();
    String typeString = null;//ww w  . j  av a  2s .co  m
    if (type.equals(DocumentEvent.EventType.CHANGE)) {
        typeString = "Change";
    } else if (type.equals(DocumentEvent.EventType.INSERT)) {
        typeString = "Insert";
    } else if (type.equals(DocumentEvent.EventType.REMOVE)) {
        typeString = "Remove";
    }
    System.out.println("Type  : " + typeString);
    Document documentSource = documentEvent.getDocument();
    Element rootElement = documentSource.getDefaultRootElement();
    DocumentEvent.ElementChange change = documentEvent.getChange(rootElement);
    System.out.println("Change: " + change);
}

From source file:TextAreaDemo.java

public void insertUpdate(DocumentEvent ev) {
    if (ev.getLength() != 1) {
        return;/* w w  w .ja v a 2  s.c  o  m*/
    }

    int pos = ev.getOffset();
    String content = null;
    try {
        content = textArea.getText(0, pos + 1);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    // Find where the word starts
    int w;
    for (w = pos; w >= 0; w--) {
        if (!Character.isLetter(content.charAt(w))) {
            break;
        }
    }
    if (pos - w < 2) {
        // Too few chars
        return;
    }

    String prefix = content.substring(w + 1).toLowerCase();
    int n = Collections.binarySearch(words, prefix);
    if (n < 0 && -n <= words.size()) {
        String match = words.get(-n - 1);
        if (match.startsWith(prefix)) {
            // A completion is found
            String completion = match.substring(pos - w);
            // We cannot modify Document from within notification,
            // so we submit a task that does the change later
            SwingUtilities.invokeLater(new CompletionTask(completion, pos + 1));
        }
    } else {
        // Nothing found
        mode = Mode.INSERT;
    }
}

From source file:LiveParenMatcher.java

public void insertUpdate_3(DocumentEvent de) {
    Document doc = de.getDocument();
    int offset = de.getOffset();
    int length = de.getLength();
    Segment seg = new Segment();
    try {/*  w w  w .j a v a  2s  .c o  m*/
        doc.getText(offset, length, seg); // text placed in Segment
    } catch (BadLocationException ble) {
    }

    // iterate through the Segment
    for (char ch = seg.first(); ch != seg.DONE; ch = seg.next())
        if (ch == '(' || ch == '[' || ch == '{' || ch == ')' || ch == ']' || ch == '}') {
            SwingUtilities.invokeLater(this); // will call run()
            return; // no need to check further
        }
}

From source file:LiveParenMatcher.java

public void insertUpdate_2(DocumentEvent de) {
    Document doc = de.getDocument();
    int offset = de.getOffset();
    int length = de.getLength();
    String inserted = "";
    try {/*from ww  w  . j  a v  a  2s.  c  o m*/
        inserted = doc.getText(offset, length);
    } catch (BadLocationException ble) {
    }

    for (int j = 0; j < inserted.length(); j += 1) {
        char ch = inserted.charAt(j);
        if (ch == '(' || ch == '[' || ch == '{' || ch == ')' || ch == ']' || ch == '}') {
            SwingUtilities.invokeLater(this); // will call run()
            return; // no need to check further
        }
    }
}

From source file:musiccrawler.App.java

@Override
public void insertUpdate(DocumentEvent e) {
    if (e.getLength() > 0) {
        lblError.setText(Constant.EMPTY);
    }
}

From source file:Console.java

public synchronized void insertUpdate(DocumentEvent e) {
    int len = e.getLength();
    int off = e.getOffset();
    if (outputMark > off) {
        outputMark += len;/*from w  ww.j a  va  2 s.  c  o m*/
    }
}

From source file:Console.java

public synchronized void removeUpdate(DocumentEvent e) {
    int len = e.getLength();
    int off = e.getOffset();
    if (outputMark > off) {
        if (outputMark >= off + len) {
            outputMark -= len;/*www.ja  va  2 s.c o m*/
        } else {
            outputMark = off;
        }
    }
}

From source file:Main.java

private void initComponents() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane textPane = new JTextPane();
    ((AbstractDocument) textPane.getDocument()).addDocumentListener(new DocumentListener() {
        @Override/*from  w  w  w .  j a va  2s  . c o m*/
        public void insertUpdate(final DocumentEvent de) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        StyledDocument doc = (StyledDocument) de.getDocument();
                        int start = Utilities.getRowStart(textPane, Math.max(0, de.getOffset() - 1));
                        int end = Utilities.getWordStart(textPane, de.getOffset() + de.getLength());

                        String text = doc.getText(start, end - start);

                        for (String emoticon : imageTokens) {
                            int i = text.indexOf(emoticon);
                            while (i >= 0) {
                                final SimpleAttributeSet attrs = new SimpleAttributeSet(
                                        doc.getCharacterElement(start + i).getAttributes());
                                if (StyleConstants.getIcon(attrs) == null) {
                                    switch (emoticon) {
                                    case imageToken:
                                        StyleConstants.setIcon(attrs, anImage);
                                        break;
                                    }
                                    doc.remove(start + i, emoticon.length());
                                    doc.insertString(start + i, emoticon, attrs);
                                }
                                i = text.indexOf(emoticon, i + emoticon.length());
                            }
                        }
                    } catch (BadLocationException ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }
    });

    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(300, 300));
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}