Example usage for javax.swing JFormattedTextField getCaretPosition

List of usage examples for javax.swing JFormattedTextField getCaretPosition

Introduction

In this page you can find the example usage for javax.swing JFormattedTextField getCaretPosition.

Prototype

@Transient
public int getCaretPosition() 

Source Link

Document

Returns the position of the text insertion caret for the text component.

Usage

From source file:com.haulmont.cuba.desktop.sys.vcl.DatePicker.DatePicker.java

@Override
public void setEditor(final JFormattedTextField editor) {
    final int ENTER_CODE = 10;

    editor.addKeyListener(new KeyAdapter() {
        @Override/*  ww w  .  j  ava2s .  co m*/
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == '\u007F' && editor.getCaretPosition() < format.length()) {
                editor.setCaretPosition(editor.getCaretPosition() + 1);
            }
        }

        @Override
        public void keyPressed(KeyEvent event) {
            if (ENTER_CODE == event.getKeyCode())
                try {
                    editor.commitEdit();
                } catch (ParseException e) {
                    //
                }
        }
    });

    editor.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            editor.setCaretPosition(0);
        }
    });

    super.setEditor(editor);

    if (format == null) {
        setFormats(
                Datatypes.getFormatStrings(AppBeans.get(UserSessionSource.class).getLocale()).getDateFormat());
    } else
        setFormats(format);
}