Example usage for javax.swing JComponent addKeyListener

List of usage examples for javax.swing JComponent addKeyListener

Introduction

In this page you can find the example usage for javax.swing JComponent 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

static void setLightweightDispatcher(JComponent component) {
    if (dispatcherField == null) {
        initReflection();//from   w  w  w .  j a v  a 2s . c om
    }
    try {
        Object dispatcher = newLightweightDispatcher.newInstance(component);
        enableEvents.invoke(dispatcher, MOUSE_MASK | AWTEvent.KEY_EVENT_MASK);
        dispatcherField.set(component, dispatcher);
        component.addKeyListener(new KeyAdapter() {
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MyButtonUI.java

public void installUI(JComponent c) {
    super.installUI(c);

    c.addMouseListener(this);
    c.addKeyListener(this);
}

From source file:net.lmxm.ute.gui.validation.AbstractInputValidator.java

/**
 * Instantiates a new abstract input validator.
 * /*www .  j a v a2s  .  c om*/
 * @param inputComponent the input component
 */
public AbstractInputValidator(final JComponent inputComponent) {
    super();

    this.inputComponent = inputComponent;

    inputComponent.addKeyListener(this);

    // Preload GUI components
    getMessagesDialog();
}

From source file:com.haulmont.cuba.desktop.sys.DesktopToolTipManager.java

/**
 * Register tooltip for component.//from www . j a  v  a2 s.  c o  m
 * The tooltip is displayed when a user either presses F1 on a focused component or hovers over it.
 * ToolTip text is taken from {@link javax.swing.JComponent#getToolTipText()}.
 *
 * @param component component to register
 */
public void registerTooltip(final JComponent component) {
    component.removeKeyListener(fieldKeyListener);
    component.addKeyListener(fieldKeyListener);

    component.removeMouseListener(componentMouseListener);
    component.addMouseListener(componentMouseListener);
}

From source file:net.lmxm.ute.gui.validation.AbstractInputValidator.java

/**
 * Instantiates a new abstract input validator.
 * /* w ww.j  a  va2  s.c  om*/
 * @param inputComponent the input component
 * @param validationRules the validation rules
 */
public AbstractInputValidator(final JComponent inputComponent, final ValidationRule... validationRules) {
    super();

    this.inputComponent = inputComponent;

    for (final ValidationRule validationRule : validationRules) {
        addRule(validationRule);
    }

    inputComponent.addKeyListener(this);

    // Preload GUI components
    getMessagesDialog();
}

From source file:edu.ku.brc.specify.conversion.CustomDBConverterPanel.java

/**
 * Creates a Document dbConverterListener so the UI is updated when the doc changes
 * @param uiComponent the text field to be changed
 *//*w  w  w. j  a  va 2  s  .co m*/
protected void addKeyListenerFor(final JComponent comp, final boolean checkForRet) {
    class KeyAdp extends KeyAdapter {
        private boolean checkForRetLocal = false;

        public KeyAdp(final boolean checkForRetArg) {
            this.checkForRetLocal = checkForRetArg;
        }

        @Override
        public void keyPressed(KeyEvent e) {
            updateUIControls();
            if (checkForRetLocal && e.getKeyCode() == KeyEvent.VK_ENTER) {
                doLogin();
            }
        }
    }

    comp.addKeyListener(new KeyAdp(checkForRet));
}

From source file:edu.ku.brc.af.ui.db.DatabaseLoginPanel.java

/**
 * Creates a Document listener so the UI is updated when the doc changes
 * @param uiComponent the text field to be changed
 *///ww  w .  ja  va2s  . c  om
protected void addKeyListenerFor(final JComponent comp, final boolean checkForRet) {
    class KeyAdp extends KeyAdapter {
        private boolean checkForRetLocal = false;

        public KeyAdp(final boolean checkForRetArg) {
            this.checkForRetLocal = checkForRetArg;
        }

        @Override
        public void keyReleased(KeyEvent e) {
            updateUIControls();
            if (checkForRetLocal && e.getKeyCode() == KeyEvent.VK_ENTER) {
                doLogin();
            }
        }
    }

    comp.addKeyListener(new KeyAdp(checkForRet));
}

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

/**
 * Creates the proper UI component for the Mapping item.
 * @param dbFieldType the field type//from   w w w  . j  av a  2 s .c o m
 * @param caption the caption
 * @param fieldName the name of the field
 * @param dataFieldLength the length of the definition for that field
 * @param fieldType the field type
 * @return a UI component for editing
 */
protected JComponent createUIComp(final Class<?> dbFieldTypeArg, final String caption, final String fieldName,
        final Short fieldType, final Short fieldLength, final short columns, final short rows,
        final WorkbenchTemplateMappingItem wbtmi) {
    short uiType = WorkbenchTemplateMappingItem.UNKNOWN;
    //System.out.println(wbtmi.getCaption()+" "+wbtmi.getDataType()+" "+wbtmi.getFieldLength());
    Class<?> dbFieldType = dbFieldTypeArg;
    if (dbFieldType == null) {
        // if we can't find a class for the field (i.e. Genus Species, or other 'fake' fields), we say it's a string
        dbFieldType = String.class;
    }

    JComponent comp;
    Component focusComp;

    // handle dates
    if (dbFieldType.equals(Calendar.class) || dbFieldType.equals(Date.class)) {
        //ValFormattedTextField txt = new ValFormattedTextField("Date"); 
        //txt.setColumns(columns == -1 ? DEFAULT_TEXTFIELD_COLS : columns);
        ValTextField txt = new ValTextField(columns);
        txt.getDocument().addDocumentListener(docListener);
        comp = txt;
        focusComp = comp;
        uiType = WorkbenchTemplateMappingItem.TEXTFIELD_DATE;
    }
    //        else if (dbFieldType.equals(Boolean.class)) // strings
    //        {
    //            ValCheckBox checkBox = new ValCheckBox(caption, false, false);
    //            checkBox.addChangeListener(changeListener);
    //            comp      = checkBox;
    //            focusComp = comp;
    //            uiType = WorkbenchTemplateMappingItem.CHECKBOX;
    //        }
    else if (useComboBox(wbtmi)) {
        //ValComboBox comboBox = new ValComboBox(getValues(wbtmi), true);

        final JComboBox comboBox = new JComboBox(getValues(wbtmi));
        comboBox.setName("Form Combo");
        comboBox.setEditable(true);
        comboBox.addActionListener(new ActionListener() {

            /* (non-Javadoc)
             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
             */
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (arg0.getSource() == comboBox) {
                    //System.out.println("ComboBox Action!" + ((JComboBox )arg0.getSource()).getName());
                    stateChange();
                }
            }

        });
        //           comboBox.getEditor().getEditorComponent().addFocusListener(new FocusListener() {
        //
        //            /* (non-Javadoc)
        //             * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
        //             */
        //            @Override
        //            public void focusGained(FocusEvent arg0) {
        //               System.out.println("FOCUS GAINED");
        //               
        //            }
        //
        //            /* (non-Javadoc)
        //             * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
        //             */
        //            @Override
        //            public void focusLost(FocusEvent arg0) {
        //               System.out.println("FOCUS LOST");
        //               
        //            }
        //              
        //           });
        comp = comboBox;
        focusComp = comp;
        uiType = WorkbenchTemplateMappingItem.COMBOBOX;
    } else if (useTextField(fieldName, fieldType, fieldLength)) {
        ValTextField txt = new ValTextField(columns);
        txt.getDocument().addDocumentListener(docListener);
        txt.setInputVerifier(new LengthInputVerifier(caption, fieldLength));
        comp = txt;
        focusComp = comp;
        uiType = WorkbenchTemplateMappingItem.TEXTFIELD;

    } else {
        JScrollPane taScrollPane = createTextArea(columns, rows);
        ((JTextArea) taScrollPane.getViewport().getView())
                .setInputVerifier(new LengthInputVerifier(caption, fieldLength));
        comp = taScrollPane;
        focusComp = taScrollPane.getViewport().getView();
        uiType = WorkbenchTemplateMappingItem.TEXTAREA;
    }

    wbtmi.setFieldType(uiType);

    focusComp.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
            selectControl(e.getSource());
        }

        public void focusLost(FocusEvent e) {
            //stateChange();
        }
    });

    comp.setEnabled(!readOnly);
    focusComp.setEnabled(!readOnly);

    comp.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (!readOnly) {
                if ((e.isControlDown() || e.isMetaDown()) && e.getKeyCode() == KeyEvent.VK_N) {
                    workbenchPane.addRowAfter();
                }
            }
            super.keyTyped(e);
        }

    });
    return comp;
}