Example usage for javax.swing JFormattedTextField setCaretPosition

List of usage examples for javax.swing JFormattedTextField setCaretPosition

Introduction

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

Prototype

@BeanProperty(bound = false, description = "the caret position")
public void setCaretPosition(int position) 

Source Link

Document

Sets the position of the text insertion caret for the TextComponent.

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 . jav  a  2s  .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);
}