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:nz.govt.natlib.ndha.manualdeposit.ManualDepositMain.java

private void addHotKeyListener(final Container container) {
    final Component[] components = container.getComponents();
    for (int i = 0; i < components.length; i++) {
        final Component component = components[i];
        component.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(final java.awt.event.KeyEvent evt) {
                checkHotKey(evt);/*from   ww  w. j  a  v  a  2s  .c  o m*/
            }
        });
        if (components[i] instanceof Container) {
            addHotKeyListener((Container) component);
        }
    }
}

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.
 *///from ww  w .  j ava  2s.c o  m
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.");
    }
}