Prezados fiz esse exemplo para ilustrar o meu problema, eu tenho 6 JTextfield e 1 JButton.
Inicia no jt01, eu do enter ele vai para o jt02, eu do enter ele vai para o jt03, eu to enter ele vai para o jbotao.
quando eu do enter no jbotao, ele aparece um JOptionPane e eu faço um jt04.requestFocusInWidonws(). Então deveria ir para o jt04, acontece que ele está indo para o jt05, sinceramente ja tentei 500 mil coisas porem não consegui fazer ele ir para o jt04. Os colegas deste forum pode me dar uma luz?
Edit### No JoptionPane eu teclo Enter tambem… se eu usar o espaco ele vai certo, porem se eu usar o enter ele nao faz o que estou tentando…
Possuo a Classe Avancar:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package AvancarRecuar;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
/**
*
* @author Rodrigo
*/
public class AvancarListener implements KeyListener {
JComponent AvancarENTER = null;
JComponent RecuarESC = null;
public AvancarListener(JComponent RecuarESC, JComponent AvancarENTER) {
this.RecuarESC = RecuarESC;
this.AvancarENTER = AvancarENTER;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 10 && AvancarENTER != null) {
AvancarENTER.requestFocus();
} else if (e.getKeyCode() == 27 && RecuarESC != null) {
RecuarESC.requestFocus();
}
}
}
e possuo o JFrame:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package AvancarRecuar;
import javax.swing.JOptionPane;
/**
*
* @author Rodrigo
*/
public class FrameTeste extends javax.swing.JFrame {
/**
* Creates new form FrameTeste
*/
public FrameTeste() {
initComponents();
jt01.addKeyListener(new AvancarListener(null, jt02));
jt02.addKeyListener(new AvancarListener(jt01, jt03));
jt03.addKeyListener(new AvancarListener(jt02, jBotao));
jt04.addKeyListener(new AvancarListener(jBotao, jt05));
jt05.addKeyListener(new AvancarListener(jt04, jt06));
jt06.addKeyListener(new AvancarListener(jt05, null));
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jt02 = new javax.swing.JTextField();
jt01 = new javax.swing.JTextField();
jt03 = new javax.swing.JTextField();
jBotao = new javax.swing.JButton();
jt04 = new javax.swing.JTextField();
jt05 = new javax.swing.JTextField();
jt06 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jBotao.setText("Executar");
jBotao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBotaoActionPerformed(evt);
}
});
jBotao.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jBotaoKeyReleased(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jt01, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addComponent(jt02)
.addComponent(jt03, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addComponent(jBotao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jt04, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addComponent(jt05)
.addComponent(jt06, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jt01, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jt02, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jt03, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBotao)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jt04, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jt05, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jt06, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(110, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jBotaoActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null, "Botao Executando");
// TODO add your handling code here:
}
private void jBotaoKeyReleased(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == 10) {
jBotao.doClick();
jt04.requestFocusInWindow();
} else if (evt.getKeyCode() == 27) {
jt03.requestFocusInWindow();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrameTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrameTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrameTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrameTeste.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameTeste().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jBotao;
private javax.swing.JTextField jt01;
private javax.swing.JTextField jt02;
private javax.swing.JTextField jt03;
private javax.swing.JTextField jt04;
private javax.swing.JTextField jt05;
private javax.swing.JTextField jt06;
// End of variables declaration
}