简体   繁体   中英

how to add global keyevent JComponent

anyone know how to add Component action key default ?

i hear about UImanager actionMap, but im not sure i have 3 combo box and 2 text field and 1 table its very wasting time to add each component a key listener press ESC to dispose Dialog like

     KeyAdapter key=new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            int x=JOptionPane.showConfirmDialog(null, "EXIT APP ?", null, JOptionPane.YES_NO_OPTION);
            if(x==JOptionPane.YES_OPTION)
            {
                dialog.setVisible(false);
                dialog.dispose();
            }
        }            
    };
    combo1.addKeyListener(key);
    combo2.addKeyListener(key);
    combo3.addKeyListener(key);
    table.addKeyListener(key);
    dialog.addKeyListener(key);
    text1.addKeyListener(key);
    text2.addKeyListener(key);

any solution to make default key if i press ESC from any Component JDialog will dispose ?

I was just playing with code to do this last night. So you get lucky with some spoon fed code.

Don't forget to read the Key Bindings tutorial so you understand the solution.

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;

/**
**  This class will close a JDialog (or a window) when the Escape key is used.
*/
public class EscapeAction extends AbstractAction
{
    public void actionPerformed(ActionEvent e)
    {
        Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        SwingUtilities.windowForComponent(c).dispose();
    }

    private static void createAndShowGUI()
    {
        JDialog dialog = new JDialog();

        JMenuBar menuBar = new JMenuBar();
        dialog.setJMenuBar( menuBar );

        JMenu menu = new JMenu( "File" );
        menuBar.add( menu );
        menu.add( new JMenuItem("FileMenuA") );
        JMenu subMenu = new JMenu( "SubFileMenu" );
        menu.add( subMenu );
        subMenu.add( new JMenuItem("SubFileMenuA") );

        menu.add( new JMenuItem("FileMenuB") );
        menu.add( new JMenuItem("FileMenuC") );

        JPopupMenu popup = new JPopupMenu();
        popup.add( new JMenuItem("SubMenuA") );
        popup.add( new JMenuItem("SubMenuB") );
        popup.add( new JMenuItem("SubMenuC") );
        popup.add( new JMenuItem("SubMenuD") );

        String[] items = { "Select Item", "Color", "Shape", "Fruit" };
        JComboBox<String> comboBox = new JComboBox<String>( items );
        dialog.add(comboBox, BorderLayout.NORTH);

        JTextField textField = new JTextField("Right Click For Popup");
        textField.setComponentPopupMenu(popup);
        dialog.add(textField);

        dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        dialog.setSize(200, 200);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible( true );

        //  Add the Key Bindings to the content pane for the EscapeAction

        JPanel contentPane = (JPanel)dialog.getContentPane();
        String escapeText = "ESCAPE";
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(escapeText);
        contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, escapeText);
        contentPane.getActionMap().put(escapeText, new EscapeAction());
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM