Gui Java Keyboard events

0

I have the following code

 JPanel pb = new JPanel();
    JLabel b = new JLabel("Buscar por ...");
    JTextField bt = new JTextField(10);
    JButton bb = new JButton("Lupa");

    // bb.setBackground(color);
    bb.addActionListener(new Busquedad());
    bb.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            //Busquedad
        }

    });

The Search class collects the text written in a JTextField and performs a search in the database.

My problem is that I want to be able to execute that if I click the button or if I use tab + enter, that is to say, that when using the keyboard perform the search as if I clicked

    
asked by dani t 14.07.2017 в 17:12
source

1 answer

2

For that you must add an event to your JTextField in the following way:

bt.addKeyListener(new KeyAdapter() {
    @Override
    public void keyTyped(KeyEvent e) {
        JOptionPane.showMessageDialog(null, "HOLA");
        if(e.getKeyCode() == KeyEvent.VK_ENTER) {
            //BUSQUEDA
        }
    }
});

This means that after entering the search parameter press the enter key, the event will trigger

    
answered by 14.07.2017 в 17:57