[Resolvido] JTextField não captura tecla Enter

4 respostas
marciosouzajunior

Pessoal, estou utilizando o seguinte trecho de código para capturar a tecla Enter:

componente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {

                 if (evt.getKeyCode() == KeyEvent.VK_ENTER)
                       JOptionPane.showMessageDialog(null, "ok");

            }
        });

Mas ele não reconhece e penso que seja porque no construtor do programa eu executo o seguinte código
para mudar o foco dos componentes pela tecla Enter:

// Pular componentes com Enter
        Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
        set.add(KeyStroke.getKeyStroke("ENTER"));
      KeyboardFocusManager.getCurrentKeyboardFocusManager().setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

Alguém já passou por este problema?

4 Respostas

Fexx

Já passei por isso, e é exatamente o que vc está dizendo.

Ao colocar isso.

// Pular componentes com Enter  
Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));  
set.add(KeyStroke.getKeyStroke("ENTER"));  
yboardFocusManager.getCurrentKeyboardFocusManager().setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

Ele troca o enter pelo tab, então o evento não conhece, acredito que o evento acha que o enter é um tab e não faz nada.

Fexx

Se alguém sabe como resolver, diz ai.

marciosouzajunior

Ae Fexx, achei uma resposta neste fórum:

http://www.velocityreviews.com/forums/t137452-tab-not-generating-keyevent.html

Onde diz o seguinte:

The Tab key is used by the KeyboardFocusManager as a focus traversal key
and therefore will not be passed on to the component. If you want to use
Tab within the JTextField, you will need to define a different set of
keys (which excludes the Tab key) for the component’s focus traversal
keys set. Look at the API docs for the method setFocusTraversalKeys(int
,Set) of the java.awt.Component class, and the documentation for the
java.awt.KeyboardFocusManager class in general.

Sendo assim, eu usei o seguinte trecho de código postado no mesmo fórum:

componente.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
        componente.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);

Funcionou certinho aqui.

Fexx

marciosouzajunior:
Ae Fexx, achei uma resposta neste fórum:

http://www.velocityreviews.com/forums/t137452-tab-not-generating-keyevent.html

Onde diz o seguinte:

The Tab key is used by the KeyboardFocusManager as a focus traversal key
and therefore will not be passed on to the component. If you want to use
Tab within the JTextField, you will need to define a different set of
keys (which excludes the Tab key) for the component’s focus traversal
keys set. Look at the API docs for the method setFocusTraversalKeys(int
,Set) of the java.awt.Component class, and the documentation for the
java.awt.KeyboardFocusManager class in general.

Sendo assim, eu usei o seguinte trecho de código postado no mesmo fórum:

componente.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
        componente.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);

Funcionou certinho aqui.

Pow valew cara. mais um problema resolvido kkk

Abraços.

Fica com DEUS.

Criado 21 de março de 2012
Ultima resposta 21 de mar. de 2012
Respostas 4
Participantes 2