How to use mouse click and keyboard ENTER?

Asked

Viewed 2,888 times

2

How to include in my class an event that gives me the possibility to click on JButton and he show a JOptionPane? And in the same way JButton, I press the ENTER and he show me the same JOptionPane?

2 answers

4

Click-through events can be captured through a ActionListener. The pressing of a certain key can be obtained with KeyEventDispatcher next to the KeyboardFocusManager.

This answer of utluiz explains why it is better to use KeyboardFocusManager instead of adding a KeyListener to your button. Although both serve to capture keyboard events, the KeyListener does not work if the focus is on another component.

A simple example that displays a JOptionPane when the key ENTER is pressed or when there is the event of click in the JButton:

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class MeuJFrame extends JFrame {

    public MeuJFrame() {
        setSize(300, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JButton btn = new JButton("Mostrar JOptionPane");
        getContentPane().add(btn);

        // Quando clicado
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mostrarJOptionPane();
            }
        });

        // Quando uma tecla for pressionada (Nesse exemplo escolhi a tecla 'B').
        KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addKeyEventDispatcher(new KeyEventDispatcher() {
                @Override
                public boolean dispatchKeyEvent(KeyEvent event) {
                    if(event.getID() == KeyEvent.KEY_RELEASED 
                       && event.getKeyCode() == KeyEvent.VK_ENTER){
                           mostrarJOptionPane();
                           return true;
                     }
                     return false;
                }
        }); 
    }

    private void mostrarJOptionPane(){
        JOptionPane.showMessageDialog(null, "Hello World.");
    }

    public static void main(String[] args) {
        new MeuJFrame().setVisible(true);
    }  
}

Like the KeyboardFocusManager captures all events, you need to make a restriction to do something only when a key is pressed and released (KeyEvent#KEY_RELEASED), therefore this passage:

if(event.getID() == KeyEvent.KEY_RELEASED ...

Otherwise, an action will be triggered (in case, show the JOptionPane) every event captured: KeyEvent#KEY_RELEASED, KeyEvent#KEY_PRESSED and KeyEvent#KEY_TYPED.

  • I can apply this same behavior to a JFRAME, instead of just a button?

  • @pmargreff which behavior?

  • Using the keyboardFocusManager, I need to implement but for some reasons I haven’t found any of the answers helped me.

  • I don’t know, I don’t understand what you want to do.

0

I took the click of 'Enter' in a textfield in javafx like this:

   private void clikBusc(KeyEvent event) {

        if (event.getCode() == KeyCode.ENTER) {
            System.out.println("deu certo");
        }
    }

So whenever I click enter inside the textField, it will enter the if, and execute what is put inside!

  • Please read the other answer, which rightly says that using Keyevent for the case of this question is a bad practice. And a little tip: Just because it works, doesn’t mean it’s right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.