Java JButton wireComponentWithButton(JComponent component, final JButton button)

Here you can find the source of wireComponentWithButton(JComponent component, final JButton button)

Description

wire Component With Button

License

Apache License

Declaration

public static void wireComponentWithButton(JComponent component, final JButton button) 

Method Source Code

//package com.java2s;
/*//from  ww  w.j  a va  2 s . c  o  m
 * Copyright 2007 - 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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

public class Main {
    public static void wireComponentWithButton(JComponent component, final JButton button) {
        component.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() > 1) {
                    if (button.isEnabled()) {
                        button.doClick();
                    }
                }
            }
        });
        component.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (e.getKeyChar() == '\n') {
                    if (button.isEnabled()) {
                        button.doClick();
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent arg0) {
            }

            @Override
            public void keyPressed(KeyEvent arg0) {
            }
        });
    }
}

Related

  1. setupAutoRepeat(final JButton button, final int intervalMs)
  2. setWFActionMap(Vector actionButtons, String action, Map wfActionMap)
  3. showToolTip(final boolean show, final JWindow tip, final JButton boton, final JLabel tipText)
  4. showToolTip(final boolean show, final JWindow tip, final JButton boton, final JLabel tipText)
  5. toolBarSeparator(JButton button, Icon icon)