Un amigo me paso este codigo para cuando necesitamos navegar en nuestra pantalla con el tabulador espero les sirva
package pruebasalgoritmos; import java.awt.KeyEventDispatcher; import java.awt.KeyboardFocusManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; /* * @author Salvador.... * @version 1.1 24/12/2010 */ public class KeyBoardFocusManager extends JFrame { private static final long serialVersionUID = 6236724848006577161L; JPanel panel; private JLabel lbClave=new JLabel("Clave"); private JLabel lbNombre=new JLabel("Nombre"); private JLabel lbPaterno=new JLabel("Apellido Paterno"); private JLabel lblMaterno = new JLabel("Apellido Materno"); private JTextField txtClave=new JTextField (); private JTextField txtNombre=new JTextField (); private JTextField txtPaterno=new JTextField (); private JTextField txtMaterno = new JTextField(); private JButton btnNuevo= new JButton("Nuevo"); private JButton btnSalir= new JButton("Salir"); public KeyBoardFocusManager() { super("KeyboardFocusManager" ); panel= new JPanel(); panel.setLayout(null); panel.setVisible(true); lbClave.setBounds(80,40,100,20); panel.add(lbClave); txtClave.setName("txtClave"); txtClave.setBounds(200,40,100,20); panel.add(txtClave); lbNombre.setBounds(80,70,100,20); panel.add (lbNombre); txtNombre.setBounds (200,70,100,20); panel.add(txtNombre); lbPaterno.setBounds(80,100,150,20); panel.add (lbPaterno); txtPaterno.setBounds (200,100,100,20); panel.add(txtPaterno); lblMaterno.setBounds(80,130,150,20); panel.add (lblMaterno); txtMaterno.setBounds (200,130,100,20); panel.add(txtMaterno); btnNuevo.setBounds (350,40,90,20); panel.add(btnNuevo); btnSalir.setBounds (350,70,90,20); panel.add(btnSalir); //Evento KeyEventDispatcher el cual hace que funcione la tecla de TABULADOR KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() { public boolean dispatchKeyEvent(KeyEvent evento) { //int tecla = evento.getKeyCode(); if(evento.getKeyCode() == KeyEvent.VK_TAB) { validaVacio(); } if (evento.getKeyCode()==KeyEvent.VK_ENTER) { validaVacio(); } return false; } }); btnNuevo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evento) { validaVacio(); } }); btnSalir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evento) { System.exit(0); } }); this.getContentPane().add(panel); this.setVisible(true); this.setSize(500,250); this.setLocation(200,100); } private void validaVacio() { if (txtClave.getText().equals("")) { JOptionPane.showMessageDialog(null, "El campo esta vacio"); txtClave.requestFocus(); }else{ txtMaterno.requestFocus(); } } @SuppressWarnings("deprecation") public static void main(String []soeck) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); new KeyBoardFocusManager().show(); } }
Publicar un comentario