Example usage for javax.swing.event DocumentEvent getDocument

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

Introduction

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

Prototype

public Document getDocument();

Source Link

Document

Gets the document that sourced the change event.

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   www . j a  va  2 s . c om
        }

        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:AddingDocumentListenerJTextFieldSample.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("Default Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);

    DocumentListener documentListener = new DocumentListener() {
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);/*from www.  ja v  a 2 s.  com*/
        }

        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            String typeString = null;
            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.print("Type : " + typeString);
            Document source = documentEvent.getDocument();
            int length = source.getLength();
            System.out.println("Length: " + length);
        }
    };
    textField.getDocument().addDocumentListener(documentListener);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField nameTextField = new JTextField();
    frame.add(nameTextField, BorderLayout.NORTH);
    frame.add(new JTextField(), BorderLayout.SOUTH);

    DocumentListener documentListener = new DocumentListener() {
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);/* ww  w  .  j  a  v a  2 s  . c o m*/
        }

        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            String typeString = null;
            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.print("Type  :   " + typeString + " / ");
            Document source = documentEvent.getDocument();
            int length = source.getLength();
            try {
                System.out.println("Contents: " + source.getText(0, length));
            } catch (BadLocationException badLocationException) {
                System.out.println("Contents: Unknown");
            }
        }
    };
    nameTextField.getDocument().addDocumentListener(documentListener);

    frame.setSize(250, 100);
    frame.setVisible(true);
}

From source file:JTextFieldSample.java

public static void main(String args[]) {
    String title = (args.length == 0 ? "TextField Listener Example" : args[0]);
    JFrame frame = new JFrame(title);
    Container content = frame.getContentPane();

    JPanel namePanel = new JPanel(new BorderLayout());
    JLabel nameLabel = new JLabel("Name: ");
    nameLabel.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField nameTextField = new JTextField();
    nameLabel.setLabelFor(nameTextField);
    namePanel.add(nameLabel, BorderLayout.WEST);
    namePanel.add(nameTextField, BorderLayout.CENTER);
    content.add(namePanel, BorderLayout.NORTH);

    JPanel cityPanel = new JPanel(new BorderLayout());
    JLabel cityLabel = new JLabel("City: ");
    cityLabel.setDisplayedMnemonic(KeyEvent.VK_C);
    JTextField cityTextField = new JTextField();
    cityLabel.setLabelFor(cityTextField);
    cityPanel.add(cityLabel, BorderLayout.WEST);
    cityPanel.add(cityTextField, BorderLayout.CENTER);
    content.add(cityPanel, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Command: " + actionEvent.getActionCommand());
        }/*ww w  . jav  a 2 s .c  om*/
    };
    nameTextField.setActionCommand("Yo");
    nameTextField.addActionListener(actionListener);
    cityTextField.addActionListener(actionListener);

    KeyListener keyListener = new KeyListener() {
        public void keyPressed(KeyEvent keyEvent) {
            printIt("Pressed", keyEvent);
        }

        public void keyReleased(KeyEvent keyEvent) {
            printIt("Released", keyEvent);
        }

        public void keyTyped(KeyEvent keyEvent) {
            printIt("Typed", keyEvent);
        }

        private void printIt(String title, KeyEvent keyEvent) {
            int keyCode = keyEvent.getKeyCode();
            String keyText = KeyEvent.getKeyText(keyCode);
            System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar());
        }
    };
    nameTextField.addKeyListener(keyListener);
    cityTextField.addKeyListener(keyListener);

    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            if ((text.length() != 0) && !(text.equals("Exit"))) {
                Runnable runnable = new Runnable() {
                    public void run() {
                        JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog",
                                JOptionPane.ERROR_MESSAGE);
                    }
                };
                SwingUtilities.invokeLater(runnable);
                return false;
            } else {
                return true;
            }
        }
    };
    nameTextField.setInputVerifier(verifier);
    cityTextField.setInputVerifier(verifier);

    DocumentListener documentListener = new DocumentListener() {
        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            String typeString = null;
            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.print("Type  :   " + typeString + " / ");
            Document source = documentEvent.getDocument();
            int length = source.getLength();
            try {
                System.out.println("Contents: " + source.getText(0, length));
            } catch (BadLocationException badLocationException) {
                System.out.println("Contents: Unknown");
            }
        }
    };
    nameTextField.getDocument().addDocumentListener(documentListener);
    cityTextField.getDocument().addDocumentListener(documentListener);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:Main.java

public void disableIfEmpty(DocumentEvent e) {
    button.setEnabled(e.getDocument().getLength() > 0);
}

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:Main.java

public Main() {
    listener = new DocumentListener() {
        Document source = null;//  w  w w.  ja  v a  2s  . co  m

        protected void update(Document doc) {
            if (source == null) {
                System.out.println("Update");
                source = doc;
                if (source.equals(input.getDocument())) {
                    input2.setText(input.getText());
                } else if (source.equals(input2.getDocument())) {
                    input.setText(input2.getText());
                }
                source = null;
            }
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            update(e.getDocument());
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            update(e.getDocument());
        }

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

    input.getDocument().addDocumentListener(listener);
    input2.getDocument().addDocumentListener(listener);

    input.setColumns(4);
    input2.setColumns(4);

    input.setValue(0.0);
    JPanel panel = new JPanel();
    panel.add(input);
    panel.add(input2);

    JOptionPane.showMessageDialog(null, panel);
}

From source file:com.croer.javaorange.diviner.SimpleOrangeTextPane.java

protected void fireText(DocumentEvent e) {
    Document document = e.getDocument();
    String text = "";
    try {//from w  ww. j av  a  2 s. c  o  m
        text = document.getText(0, document.getLength());
    } catch (BadLocationException ex) {
        Logger.getLogger(SimpleOrangeTextPane.class.getName()).log(Level.SEVERE, null, ex);
    }
    firePropertyChange("text", null, text);
}

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 {//from ww  w. j av  a2  s .  c  om
        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 {// w  w  w .java  2  s . c om
        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
        }
    }
}