Example usage for java.awt Component addKeyListener

List of usage examples for java.awt Component addKeyListener

Introduction

In this page you can find the example usage for java.awt Component 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:Main.java

public static void addKeyListener(KeyAdapter listener, Component... components) {
    for (Component component : components) {
        component.addKeyListener(listener);
    }//  ww  w. j  a v  a2  s  .  c  o m
}

From source file:Main.java

/**
 * Attaches a key event listener to given component, simulating a button click upon
 * pressing enter within the context./*ww  w  . j ava2  s.  co  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

/**
 * Attaches a key event listener to given component, disposing of the given window
 * upon pressing escape within the context.
 * //from ww  w .j  a v  a2s.  c om
 * @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 addKeyListener(Component p_c, KeyListener p_listener) {
    if (p_c != null) {
        p_c.addKeyListener(p_listener);

        Component[] cs = null;//from ww  w.  j  av  a 2 s  .  c  o  m

        if (p_c instanceof Container) {
            cs = ((Container) p_c).getComponents();
        }

        for (int i = 0; i < cs.length; i++) {
            addKeyListener(cs[i], p_listener);
        }
    }
}

From source file:Main.java

public static void addKeyAdapterRecursively(final Container container, final KeyAdapter keyAdapter) {
    container.addKeyListener(keyAdapter);
    for (int i = 0; i < container.getComponentCount(); i++) {
        final Component child = container.getComponent(i);
        if (child instanceof Container) {
            addKeyAdapterRecursively((Container) child, keyAdapter);
        }/*from  w  w  w  . j  a  v a2  s  . c o  m*/
        child.addKeyListener(keyAdapter);
    }
}

From source file:Main.java

/**
 * Installs the key listener in the tree of components where the argument component is included, starting in the
 * first parent <i>JFrame</i> or <i>JDialog</i> parent.
 * //from w w w .j a va2 s. c  om
 * @param cmp The starting components in the tree.
 * @param keyListener The key listener to install.
 * @param removePrevious A boolean indicating whether previous key listeners should be removed.
 */
public static void installKeyListener(Component cmp, KeyListener keyListener, boolean removePrevious) {
    Component parent = getFirstParentFrameOrDialog(cmp);
    if (parent == null) {
        parent = cmp;
    }
    List<Component> components = getAllComponents(parent);
    for (Component component : components) {
        if (removePrevious) {
            removeKeyListeners(component);
        }
        component.addKeyListener(keyListener);
    }
}

From source file:Main.java

/**
 * Inserts the key listener at the particular index in the listeners' chain.
 *
 * @param component/*from   w  w  w .j  a  va 2s  . com*/
 * @param l
 * @param index
 */
public static void insertKeyListener(Component component, KeyListener l, int index) {
    KeyListener[] listeners = component.getKeyListeners();

    for (KeyListener listener : listeners) {
        component.removeKeyListener(listener);
    }

    for (int i = 0; i < listeners.length; i++) {
        KeyListener listener = listeners[i];

        if (index == i) {
            component.addKeyListener(l);
        }

        component.addKeyListener(listener);
    }

    // inex is too large, add to the end.
    if (index > listeners.length - 1) {
        component.addKeyListener(l);
    }
}

From source file:de.codesourcery.jasm16.ide.ui.viewcontainers.EditorContainer.java

public static final void addEditorCloseKeyListener(Component comp, final IEditorView view) {
    comp.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_W && (e.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0) {
                System.out.println("*** Closing editor " + view + " ***");
                if (view.hasViewContainer()) {
                    view.getViewContainer().disposeView(view);
                } else {
                    view.dispose();/*from w w  w  .j  a va2  s.  c om*/
                }
            }
        }
    });
}

From source file:net.sf.firemox.ui.wizard.Wizard.java

/**
 * Add a component to be listened.//w w w  .  j a  v a  2  s  . c  o m
 * 
 * @param component
 *          component to add
 */
protected final void addCheckValidity(Component component) {
    component.addKeyListener(this);
}

From source file:ve.zoonosis.model.bean.AbstractForm.java

protected void agregarValidEvent(Component o, ValidarFormularioActionListener evt) {
    if (o instanceof JTextComponent) {
        o.addKeyListener(evt);
    } else if (o instanceof JComboBox) {
        ((JComboBox) o).addActionListener(evt);
    } else if (o instanceof JDateChooser) {
        ((JTextField) ((JDateChooser) o).getDateEditor().getUiComponent()).addKeyListener(evt);
        ((JDateChooser) o).getDateEditor().addPropertyChangeListener(evt);
    }//w w  w.java 2s  .c om
}