Example usage for java.awt Component getKeyListeners

List of usage examples for java.awt Component getKeyListeners

Introduction

In this page you can find the example usage for java.awt Component getKeyListeners.

Prototype

public synchronized KeyListener[] getKeyListeners() 

Source Link

Document

Returns an array of all the key listeners registered on this component.

Usage

From source file:KeyUtils.java

public static void pressKey(Component component) {
    if (component.getKeyListeners().length > 0) {
        KeyEvent event = new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, 1, 32, (char) 32);
        for (int i = 0; i < component.getKeyListeners().length; i++) {
            KeyListener keyListener = component.getKeyListeners()[i];
            keyListener.keyPressed(event);
        }/* w  w  w . ja va  2  s .co m*/
    }

    if (JComponent.class.isInstance(component)) {
        KeyStroke keyStroke = KeyStroke.getKeyStroke(32, 1);
        final ActionListener actionForKeyStroke = ((JComponent) component).getActionForKeyStroke(keyStroke);
        if (actionForKeyStroke != null) {
            actionForKeyStroke.actionPerformed(new ActionEvent(component, KeyEvent.KEY_PRESSED, ""));
        }
    }
}

From source file:Main.java

/**
 * Inserts the key listener at the particular index in the listeners' chain.
 *
 * @param component//  w  ww.j a v a  2  s .c o  m
 * @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:Main.java

/**
 * Remove key listeners form the argument component.
 * //from w ww  .j av  a 2 s . c  om
 * @param cmp The component.
 */
public static void removeKeyListeners(Component cmp) {
    KeyListener[] listeners = cmp.getKeyListeners();
    if (listeners != null) {
        for (KeyListener listener : listeners) {
            cmp.removeKeyListener(listener);
        }
    }
}

From source file:edu.ku.brc.specify.datamodel.busrules.DeterminationBusRules.java

/**
 * @param kl//from w  w  w  .  ja  va2 s  . c o  m
 * @param comp
 * 
 * Adds KeyListener to comp if it is not already a listener.
 * (This method is overkill given the current way listeners are set up.)
 */
protected void addListenerIfNecessary(final KeyListener kl, final Component comp) {
    boolean fnd = false;
    for (KeyListener existingKl : comp.getKeyListeners()) {
        if (existingKl == kl) {
            fnd = true;
            break;
        }
    }
    if (!fnd) {
        comp.addKeyListener(kl);
    }
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * Removes all the focus listeners for a component.
 * @param comp the comp//ww  w .ja  v a  2 s. c  om
 */
public static void removeKeyListeners(final Component comp) {
    if (comp != null) {
        for (KeyListener l : comp.getKeyListeners()) {
            comp.removeKeyListener(l);
        }
    }
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.wbuploader.Uploader.java

/**
 * Moves to dataset cell corresponding to currently selected validation issue and starts editor.
 *//*  w ww  .jav  a  2s  .  c om*/
protected void goToMsgWBCell(final Component c, boolean stopEdit) {
    if (mainPanel == null) {
        throw new RuntimeException("Upload form does not exist.");
    }
    if (wbSS != null) {
        UploadMessage msg;
        if (c == mainPanel.getValidationErrorList()) {
            msg = (UploadMessage) mainPanel.getValidationErrorList().getSelectedValue();
        } else {
            msg = (UploadMessage) mainPanel.getMsgList().getSelectedValue();
        }

        if (msg == null) {
            logDebug("gotToMsgWBCell: null message");
            return;
        }

        if (msg.getRow() != -1) {
            if (msg.getCol() == -1) {
                wbSS.getSpreadSheet().scrollToRow(msg.getRow());
                wbSS.getSpreadSheet().getSelectionModel().clearSelection();
                wbSS.getSpreadSheet().getSelectionModel().setSelectionInterval(msg.getRow(), msg.getRow());
            } else {
                wbSS.getSpreadSheet().getSelectionModel().clearSelection();
                Rectangle rect = wbSS.getSpreadSheet().getCellRect(msg.getRow(), msg.getCol(), false);
                wbSS.getSpreadSheet().scrollRectToVisible(rect);
                if (msg instanceof UploadTableInvalidValue && msg.getCol() != -1) {
                    if (!stopEdit) {

                        wbSS.getSpreadSheet().editCellAt(msg.getRow(), msg.getCol(), null);

                        // Now, if necessary, add this as a listener to the editorComponent to
                        // allow moving to
                        // next/prev
                        // invalid cell after ENTER/TAB/UP/DOWN
                        Component editor = wbSS.getSpreadSheet().getEditorComponent();
                        boolean addListener = true;
                        if (editor != null) {
                            KeyListener[] listeners = editor.getKeyListeners();
                            for (int k = 0; k < listeners.length; k++) {
                                if (listeners[k] instanceof Uploader) {
                                    if (listeners[k] == this) {
                                        logDebug("already listening to spreadsheet editor");
                                        addListener = false;
                                        break;
                                    }
                                    // should never get here, but just in case:
                                    logDebug("removing previous listener");
                                    editor.removeKeyListener(listeners[k]);
                                }
                            }
                            if (addListener) {
                                logDebug("adding this as listener to spreadsheet editor");
                                editor.addKeyListener(this);
                                this.keyListeningTo.add(editor);
                            }
                            editor.requestFocusInWindow();
                        }
                    } else {
                        if (wbSS.getSpreadSheet().getCellEditor() != null) {
                            wbSS.getSpreadSheet().getCellEditor().stopCellEditing();
                        }
                    }
                }
            }
        }
    }
    if (mainPanel == null) {
        throw new RuntimeException("Upload form does not exist.");
    }
}