Tenho 1 campo e texto e gostaria que ele so aceitasse numeros. gostaria tambem que ele so aceitasse 5 digitos e que quando eu inserisse o 5 digito ele automaticamente executasse tab. ou seja mudava para o outro campo.
Alguem me pode dar um exemplo com codigo, supondo que o campo de texto tenha a referencia de “TEXTO1”
packagecustomJTextField;importjava.awt.Container;importjavax.swing.InputVerifier;importjavax.swing.JComponent;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JTextField;publicclassCustomJTextFieldextendsJFrame{privateJTextFieldTEXTO1,txtNome;privateStringBuffererrorCauses=null;publicCustomJTextField(){instanciateComponents();configGUI();}privatevoidinstanciateComponents(){TEXTO1=newJTextField();TEXTO1.setBounds(0,21,100,20);TEXTO1.setInputVerifier(newMyVerifier());txtNome=newJTextField();txtNome.setBounds(0,71,100,20);}privatevoidconfigGUI(){Containerc=getContentPane();c.setLayout(null);JLabellblCodigo=newJLabel("Código");lblCodigo.setBounds(0,0,150,20);c.add(lblCodigo);c.add(TEXTO1);JLabellblNome=newJLabel("Nome");lblNome.setBounds(0,50,150,20);c.add(lblNome);c.add(txtNome);setBounds(0,0,150,130);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}publicstaticvoidmain(String[]args){(newCustomJTextField()).show();}privateclassMyVerifierextendsInputVerifier{privatebooleanallowLostOfFocus=false;/* * Método implementado */publicbooleanverify(JComponentinput){errorCauses=newStringBuffer("");if(!(inputinstanceofJTextField)){errorCauses.append("Um objeto MyVerifier só deve ser "+"associado a componentes do tipo JTextField");returnfalse;}JTextFieldtxt=(JTextField)input;if(txt.getText().length()!=5){errorCauses.append("- O campo deve ter 5 digitos.");}try{Integer.parseInt(txt.getText());}catch(NumberFormatExceptione){errorCauses.append(errorCauses.length()==0?"- ":"\n- ");errorCauses.append("O campo deve ter apenas numeros.");}returnerrorCauses.length()==0;}/* * Método sobrescrito */publicbooleanshouldYieldFocus(JComponentinput){if(allowLostOfFocus)returntrue;if(verify(input))returntrue;/* * Trate aqui a entrada inválida da forma que lhe for mais * conveniente */allowLostOfFocus=true;JOptionPane.showMessageDialog(input.getParent(),errorCauses,"Erro",JOptionPane.ERROR_MESSAGE);allowLostOfFocus=false;returnfalse;}}}
Espero que isso ajude
I
igson_mendes_da_silv
packageyo;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjavax.swing.JFrame;importjavax.swing.JTextField;publicclassTesteNumericoextendsJFrame{JTextFieldtf;publicTesteNumerico(){super(" Seja bem vindo");getContentPane().setLayout(null);tf=newJTextField();tf.setBounds(50,50,120,20);tf.addKeyListener(newKeyListener(){publicvoidkeyTyped(KeyEvente){charc=e.getKeyChar();if(!Character.isDigit(c)){e.consume();}}publicvoidkeyPressed(KeyEvente){}publicvoidkeyReleased(KeyEvente){// TODO Auto-generated method stub}});getContentPane().add(tf);setSize(250,250);setVisible(true);}publicstaticvoidmain(Stringargs[]){newTesteNumerico();}}-Issoaívcfazsopraentrardadosdotiponumerico!