Example usage for javax.swing JTextField getDocument

List of usage examples for javax.swing JTextField getDocument

Introduction

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

Prototype

public Document getDocument() 

Source Link

Document

Fetches the model associated with the editor.

Usage

From source file:Main.java

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

    JPanel contentPane = new JPanel();
    contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    JTextField tfield1 = new JTextField(10);
    JTextField tfield2 = new JTextField(10);

    FocusListener tfieldListener = new FocusListener() {
        @Override//from w  w w . j a  va2 s . c om
        public void focusGained(FocusEvent fe) {
        }

        @Override
        public void focusLost(FocusEvent fe) {
            String num1 = tfield1.getText().trim();
            String num2 = tfield2.getText().trim();
            if (num1 == null || num1.equals(""))
                num1 = "0";
            if (num2 == null || num2.equals(""))
                num2 = "0";
            System.out.println(Integer.toString(Integer.parseInt(num1) + Integer.parseInt(num2)));
        }
    };

    tfield1.addFocusListener(tfieldListener);
    tfield2.addFocusListener(tfieldListener);

    ((AbstractDocument) tfield1.getDocument()).setDocumentFilter(new MyDocumentFilter());
    ((AbstractDocument) tfield2.getDocument()).setDocumentFilter(new MyDocumentFilter());

    contentPane.add(tfield1);
    contentPane.add(tfield2);

    frame.setContentPane(contentPane);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

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   ww w  . jav a2 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();
            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);//from   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 . ja va 2  s.  c  o m
    };
    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

/**
 * Adds input validator to text fields.//from w  w  w  .ja  v  a  2  s.co  m
 *
 * @param inputValidator input validator
 * @param textFields     text fields
 */
public static void addInputValidator(final DocumentListener inputValidator, final JTextField... textFields) {
    for (final JTextField textField : textFields) {
        textField.getDocument().addDocumentListener(inputValidator);
    }
}

From source file:Main.java

public static JTextField forceUpperCase(JTextField textField) {
    ((AbstractDocument) textField.getDocument()).setDocumentFilter(getUpperCaseFilter());
    return textField;
}

From source file:Main.java

public static void onTextFieldChange(JTextField field, final Runnable task) {
    field.getDocument().addDocumentListener(new DocumentListener() {

        @Override//from ww w  .  j a  v  a  2s.  c om
        public void insertUpdate(DocumentEvent e) {
            task.run();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            task.run();
        }

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

From source file:Main.java

/**
 * Same as {@link #requestFocus(Component)} but also selects all text in the text-field.
 *//*from   w w  w.  j  a va2 s  . c om*/
public static void requestFocusSelectAll(final JTextField c) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            c.requestFocusInWindow();
            int l = c.getDocument().getLength();
            if (l > 0) {
                c.setSelectionStart(0);
                c.setSelectionEnd(l);
            }
        }
    });
}

From source file:com.opendoorlogistics.speedregions.excelshp.app.FileBrowserPanel.java

public static JTextField createTextField(String initialValue, final Consumer<String> filenameChangeListener) {
    final JTextField textField = new JTextField();
    if (initialValue != null) {
        textField.setText(initialValue);
    }/*w w  w  .  j av  a 2 s .  c o  m*/

    if (filenameChangeListener != null) {
        textField.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void removeUpdate(DocumentEvent e) {
                fire();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                fire();
            }

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

            private void fire() {
                filenameChangeListener.accept(textField.getText());
            }
        });
    }
    return textField;
}

From source file:com.opendoorlogistics.core.utils.ui.FileBrowserPanel.java

private static JTextField createTextField(String initialFilename,
        final FilenameChangeListener filenameChangeListener) {
    JTextField textField = new JTextField();
    if (initialFilename != null) {
        textField.setText(initialFilename);
    }/*from  ww  w  .  j  a  va2 s.  co m*/

    if (filenameChangeListener != null) {
        textField.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void removeUpdate(DocumentEvent e) {
                fire();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                fire();
            }

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

            private void fire() {
                filenameChangeListener.filenameChanged(textField.getText());
            }
        });
    }
    return textField;
}