Control event in the field?

Asked

Viewed 118 times

1

I’m trying to treat a possible empty search when the user gives enter in a field without typing anything. I applied a keyboard event, and every time you press enter, the field performs a search.

The problem, is that if the message appears and click enter again, it will go into infinite loop, because the field has focus. You can disable the event until the message is closed or something like that ?

What I did:

import java.awt.Dimension;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Pesquisa extends JFrame {

    JTextField pesquisa = new JTextField();
    JLabel resultado = new JLabel();

    public Pesquisa() {
        setTitle("Teste !");
        add(tela());
        setSize(500, 350);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JComponent tela() {
        JPanel painel = new JPanel();
        painel.add(pesquisa);
        painel.add(resultado);
        pesquisa.setPreferredSize(new Dimension(80, 22));
        pesquisa.addActionListener((ActionEvent e)
                -> {
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher((KeyEvent e1)
                    -> {
                if (e1.getID() == KeyEvent.KEY_RELEASED && e1.getKeyCode() == KeyEvent.VK_ENTER) {
                    String conteudo = pesquisa.getText();
                    if (!(conteudo.isEmpty())) {
                        resultado.setText("Resultado: " + conteudo);
                    } else {
                        JOptionPane.showMessageDialog(null, "Digite um número !");
                    }
                } else {

                }
                return false;
            });
        });

        return painel;
    }

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

1 answer

2


If the objective is to detect the ENTER better use KeyListener:

pesquisa.addKeyListener(new KeyAdapter() {

    @Override
    public void keyReleased(KeyEvent e) {

        if(e.getKeyCode() == KeyEvent.VK_ENTER){
             String conteudo = pesquisa.getText();
             if (!(conteudo.isEmpty())) {
                 resultado.setText("Resultado: " + conteudo);
             } else {
                 JOptionPane.showMessageDialog(null, "Digite um número !");
             }
        } else {

        }
    }

});

Recommended reading: How to Write a Key Listener

  • I already thank for the optimization of the code, plus, what I mean, is that Voce uses enter for research, so far so good, however, if the user gives enter in the field, it is empty, will generate error, and if he tries to close this error (error message) clicking enter, it will go into loop, I wanted to avoid this loop, understand ?

  • @G.Araujo the solution of the answer ends the loop. Do not cascade events as you did unless you know the consequences of this. And one of the consequences of its code is precisely the loop. If you only need to validate the empty search when typing enter, you do not need to add keyboard focus event, as you did using keyboardmanager, because when closing the alert, the focus goes back to the component, which triggers this event again by opening the alert again, and so on. At my suggestion, you will only check when enter is pressed and released.

Browser other questions tagged

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