Example usage for javax.swing JTextField addKeyListener

List of usage examples for javax.swing JTextField addKeyListener

Introduction

In this page you can find the example usage for javax.swing JTextField 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.SCI.centraltoko.utility.UtilityTools.java

public static void setAutoUpperCase(final JTextField textField) {
    textField.addKeyListener(new KeyAdapter() {

        @Override//  w  w w  . j  a v a2s. c o  m
        public void keyReleased(KeyEvent e) {
            if (Character.isLetter(e.getKeyChar())) {
                convertToUpperCase((JTextField) e.getSource());
            }
        }

    });
}

From source file:com.SCI.centraltoko.utility.UtilityTools.java

public static void setNumericTextOnly(final JTextField textField) {
    textField.addKeyListener(new KeyAdapter() {

        @Override//w  w w.ja va 2 s .c  o  m
        public void keyTyped(KeyEvent e) {
            if (!Character.isDigit(e.getKeyChar()) || BAD_CHARS.indexOf(e.getKeyChar()) > -1) {
                e.consume();
            }
        }

    });

}

From source file:Main.java

private Main() {
    combobox.setEditable(true);/* w ww  . j  av  a  2s .c om*/
    JTextField editor = (JTextField) combobox.getEditor().getEditorComponent();
    editor.addKeyListener(this);
    setLayout(new FlowLayout());
    add(combobox);
    pack();
    setVisible(true);
}

From source file:Main.java

public Main() throws HeadlessException {
    setSize(200, 200);/*from   ww w  .j  ava2s  . com*/
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JLabel usernameLabel = new JLabel("Username: ");
    JTextField usernameTextField = new JTextField();
    usernameTextField.setPreferredSize(new Dimension(100, 20));
    add(usernameLabel);
    add(usernameTextField);

    usernameTextField.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            JTextField textField = (JTextField) e.getSource();
            String text = textField.getText();
            textField.setText(text.toUpperCase());
        }

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }
    });
}

From source file:com.konifar.material_icon_generator.FilterComboBox.java

private void initListener() {
    final JTextField textfield = (JTextField) this.getEditor().getEditorComponent();
    textfield.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent event) {
            switch (event.getKeyCode()) {
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_ESCAPE:
                requestFocus(false);/* ww  w.  ja  va  2  s  .c o m*/
                break;
            case KeyEvent.VK_UP:
            case KeyEvent.VK_DOWN:
                break;
            default:
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        filter(textfield.getText());
                    }
                });
            }
        }
    });

    getAccessibleContext().addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent event) {
            if (AccessibleContext.ACCESSIBLE_STATE_PROPERTY.equals(event.getPropertyName())
                    && AccessibleState.FOCUSED.equals(event.getNewValue())
                    && getAccessibleContext().getAccessibleChild(0) instanceof ComboPopup) {
                ComboPopup popup = (ComboPopup) getAccessibleContext().getAccessibleChild(0);
                JList list = popup.getList();

                if (list.getSelectedValue() != null) {
                    setSelectedItem(String.valueOf(list.getSelectedValue()));
                }
            }
        }
    });
}

From source file:com.sec.ose.osi.ui.frm.main.identification.common.JComboComponentName.java

private void init() {

    final JTextField tfComponentName = (JTextField) this.getEditor().getEditorComponent();

    tfComponentName.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            char ch = e.getKeyChar();
            if (ch != KeyEvent.VK_ENTER && ch != KeyEvent.VK_BACK_SPACE && ch != KeyEvent.VK_SPACE
                    && (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch)))
                return;

            if (ch == KeyEvent.VK_ENTER) {

                JComboComponentName.this.hidePopup();
                return;
            }// ww w. j  a  v  a2s .  c o  m
            // 1. save string
            String keyword = tfComponentName.getText();//.trim();
            updateComboBoxList(keyword);

        }
    });

    tfComponentName.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent e) {
            if (tfComponentName.getText().length() > 0)
                showPopup();
        }

        public void focusLost(FocusEvent e) {
            hidePopup();
        }
    });

}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopFilterHelper.java

@Override
public void addTextChangeListener(TextField textField, final TextChangeListener listener) {
    final JTextField dTextField = (JTextField) DesktopComponentsHelper.unwrap(textField);
    dTextField.addKeyListener(new KeyAdapter() {
        @Override//www  .  j  a v a2s .  co m
        public void keyReleased(KeyEvent e) {
            listener.textChanged(dTextField.getText());
        }
    });
}

From source file:edu.harvard.mcz.imagecapture.ui.FilteringAgentJComboBox.java

private void init() {
    // listen for loss of focus on the text field
    this.getEditor().getEditorComponent().addFocusListener(this);
    this.setEditable(true);
    final JTextField textfield = (JTextField) this.getEditor().getEditorComponent();
    textfield.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent keyEvent) {
            log.debug(keyEvent);//from w  ww  . j  a  v  a  2s  . c om
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    filter(textfield.getText(), true);
                }
            });
        }
    });

}

From source file:edu.harvard.mcz.imagecapture.ui.FilteringGeogJComboBox.java

private void init() {
    countryLimit = "";
    stateprovLimit = "";
    cachedModel = null;/*from  w w w  .  j av a  2  s .  c  o m*/
    lastTextLength = 0;
    // listen for loss of focus on the text field
    this.getEditor().getEditorComponent().addFocusListener(this);
    this.setEditable(true);
    final JTextField textfield = (JTextField) this.getEditor().getEditorComponent();
    textfield.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent keyEvent) {
            log.debug(keyEvent);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    filter(textfield.getText(), true);
                }
            });
        }
    });

}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopFilterHelper.java

@Override
public void addShortcutListener(TextField textField, final ShortcutListener listener) {
    final JTextField dTextField = (JTextField) DesktopComponentsHelper.unwrap(textField);
    final KeyStroke keyStroke = DesktopComponentsHelper.convertKeyCombination(listener.getKeyCombination());
    dTextField.addKeyListener(new KeyAdapter() {
        @Override//from ww  w . j  a  va  2s .  co m
        public void keyReleased(KeyEvent e) {
            if (ObjectUtils.equals(e.getKeyCode(), keyStroke.getKeyCode())
                    && ObjectUtils.equals(e.getModifiers(), keyStroke.getModifiers())) {
                listener.handleShortcutPressed();
            }
        }
    });
}