Example usage for javax.swing JFormattedTextField addKeyListener

List of usage examples for javax.swing JFormattedTextField addKeyListener

Introduction

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

Prototype

public synchronized void addKeyListener(KeyListener l) 

Source Link

Document

Adds the specified key listener to receive key events from this component.

Usage

From source file:com.streamhub.StreamHubLicenseGenerator.java

private static JPanel createMacAddressRow() {
    JPanel macAddressPanel = new JPanel(new FlowLayout());

    Calendar yesterday = Calendar.getInstance();
    yesterday.setTime(new Date());
    yesterday.add(Calendar.DAY_OF_MONTH, -1);

    Calendar expiry = Calendar.getInstance();
    expiry.setTime(yesterday.getTime());
    expiry.add(Calendar.DAY_OF_MONTH, 60);

    JLabel macAddressLabel = new JLabel("MAC Address:");
    JTextField macAddress = new JTextField(17);
    JLabel startDateLabel = new JLabel("Start Date");
    final JFormattedTextField startDate = new JFormattedTextField(new SimpleDateFormat(DATE_FORMAT));
    startDate.setValue(yesterday.getTime());

    JLabel expiryDateLabel = new JLabel("Expiry Date");
    final JFormattedTextField expiryDate = new JFormattedTextField(new SimpleDateFormat(DATE_FORMAT));
    expiryDate.setValue(expiry.getTime());

    startDate.addKeyListener(new KeyListener() {
        @Override/*from   www.  j  a v  a  2s  .c  om*/
        public void keyTyped(KeyEvent arg0) {
            // Get the new date and change expiry to suit
            Date date = (Date) startDate.getValue();
            Calendar tempCal = Calendar.getInstance();
            tempCal.setTime(date);
            tempCal.add(Calendar.DAY_OF_MONTH, 60);
            expiryDate.setValue(tempCal.getTime());
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });

    JComboBox edition = new JComboBox(new String[] { "web", "enterprise" });
    JLabel nameLabel = new JLabel("Name:");
    JTextField name = new JTextField(15);

    JLabel numUsersLabel = new JLabel("Num. Users:");
    JTextField numUsers = new JTextField(DEFAULT_NUM_USERS);

    macAddressPanel.add(macAddressLabel);
    macAddressPanel.add(macAddress);
    macAddressPanel.add(startDateLabel);
    macAddressPanel.add(startDate);
    macAddressPanel.add(expiryDateLabel);
    macAddressPanel.add(expiryDate);
    macAddressPanel.add(edition);
    macAddressPanel.add(nameLabel);
    macAddressPanel.add(name);
    macAddressPanel.add(numUsersLabel);
    macAddressPanel.add(numUsers);
    return macAddressPanel;
}

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  a v  a2 s .  com
        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);
}