I'm trying to handle a possible empty search, when the user gives enter in a field without typing anything. I've 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 enter an infinite loop because the field has focus. Can you disable the event until the message is closed or something like this?
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);
}
}