Java JButton addCRListener(JComponent c, final JButton b)

Here you can find the source of addCRListener(JComponent c, final JButton b)

Description

Pressing Enter on the given component will act as clicking on the given button.

License

Open Source License

Parameter

Parameter Description
c the given component
b the given button

Declaration

public static void addCRListener(JComponent c, final JButton b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComponent;

public class Main {
    /**// w  ww  .ja  va 2  s .co m
     * Pressing Enter on the given component will act as clicking 
     * on the given button.
     * 
     * @param c
     *       the given component
     * @param b
     *       the given button
     */
    public static void addCRListener(JComponent c, final JButton b) {
        c.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                    b.doClick();
                }
            }
        });
    }

    public static void addCRListener(JButton b) {
        addCRListener(b, b);
    }
}

Related

  1. addActionToButton(JButton button, final Runnable action)
  2. addEnterListener(JComponent c, final JButton b)
  3. adjustButtonMargins(Insets margin, JButton... buttons)
  4. adjustButtonWidth(JButton button, int minWidth, int minHeight)
  5. btnHover(JButton btn)