Example usage for java.awt.event KeyAdapter KeyAdapter

List of usage examples for java.awt.event KeyAdapter KeyAdapter

Introduction

In this page you can find the example usage for java.awt.event KeyAdapter KeyAdapter.

Prototype

KeyAdapter

Source Link

Usage

From source file:Main.java

public static KeyListener maxLength(JTextComponent textComponent, int length) {
    return new KeyAdapter() {
        @Override// ww  w. j av a 2s  .c  o m
        public void keyTyped(KeyEvent e) {
            if (textComponent.getText().length() >= length) {
                e.consume();
                textComponent.setText(textComponent.getText().substring(0, length));
            }
        }
    };
}

From source file:Main.java

public static void addEscapeExitListeners(final JFrame window) {
    addKeyAdapterRecursively(window, new KeyAdapter() {
        @Override/*  w  w  w  .j  a  v  a 2s  . com*/
        public void keyPressed(final KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                System.exit(0);
            }
        }
    });
}

From source file:Main.java

/**
 * Attaches a key event listener to given component, simulating a button click upon
 * pressing enter within the context.//from   w  w  w. jav  a 2 s .  c o m
 * 
 * @param context
 * @param button
 */
public static void simulateClickOnEnter(Component context, JButton button) {
    context.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER)
                button.doClick();
        }
    });
}

From source file:Main.java

/**
 * Set the text component in which only numerics (positive decimal integers
 * with character 0-9)./*from   www  .ja va 2s  .  co m*/
 * 
 * @param c
 *            the specified text component
 */
public static void setTextComponetIntegerValid(final JTextComponent c) {
    c.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() != KeyEvent.VK_BACK_SPACE)
                try {
                    Integer.valueOf(c.getText() + e.getKeyChar());
                } catch (NumberFormatException nfe) {
                    e.consume();
                    Toolkit.getDefaultToolkit().beep();
                }
        }
    });
}

From source file:Main.java

/**
 * Attaches a key event listener to given component, disposing of the given window
 * upon pressing escape within the context.
 * //w  w  w  . j a  v a  2  s  .  c o m
 * @param context
 * @param button
 */
public static void simulateExitOnEscape(Component context, JFrame window) {
    context.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                for (WindowListener wl : window.getWindowListeners()) {
                    wl.windowClosing(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
                }

                if (window != null)
                    window.dispose();
            }
        }
    });
}

From source file:Main.java

public static void addEscapeExitListeners(final JDialog window) {
    addKeyAdapterRecursively(window, new KeyAdapter() {
        @Override// w ww .j a  v  a  2 s.  c o  m
        public void keyPressed(final KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                System.exit(0);
            }
        }
    });
}

From source file:Main.java

MyTextField(int len) {
    super(len);//from  w w  w  . j  av a2 s.c o m
    addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent evt) {
            int key = evt.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
                transferFocus();
        }
    });
}

From source file:KeyTextComponent.java

public KeyTextComponent() {
    setBackground(Color.CYAN);/*ww w  .j  a  va  2  s  .com*/
    KeyListener internalKeyListener = new KeyAdapter() {
        public void keyPressed(KeyEvent keyEvent) {
            if (actionListenerList != null) {
                int keyCode = keyEvent.getKeyCode();
                String keyText = KeyEvent.getKeyText(keyCode);
                ActionEvent actionEvent = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, keyText);
                actionListenerList.actionPerformed(actionEvent);
            }
        }
    };
    MouseListener internalMouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            requestFocusInWindow();
        }
    };
    addKeyListener(internalKeyListener);
    addMouseListener(internalMouseListener);
}

From source file:Main.java

public Main() {
    DefaultTableModel model = new DefaultTableModel(0, 5) {
        @Override/*from w  w w.j  ava2 s . com*/
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    JTable table = new JTable(model);
    for (int i = 0; i < 20; i++) {
        model.addRow(new String[] { i + ".1", i + ".2", i + ".3", i + ".4", i + ".5", });
    }
    add(table, BorderLayout.CENTER);
    table.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            System.out.println("pressed");
            char key = e.getKeyChar();
            int selectedColumn = table.getSelectedColumn();
            for (int i = 0; i < model.getRowCount(); i++) {
                String value = (String) model.getValueAt(i, selectedColumn);
                model.setValueAt(value + key, i, selectedColumn);
            }
        }
    });
}

From source file:Main.java

public Main() throws HeadlessException {
    setSize(200, 200);//from www  .  j a  v  a2  s.  c  o m
    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) {
        }
    });
}