Pessoal, estou com problemas para limitar o tamanho de um jtextfield!!!
Alguem pode me ajudar??
packageinterfaces;importjavax.print.attribute.AttributeSet;importjavax.swing.text.PlainDocument;importjavax.swing.text.*;classTamanhoMaximoextendsPlainDocument{privateintiMaxLength;publicTamanhoMaximo(intmaxlen){super();iMaxLength=maxlen;}publicvoidinsertString(intoffset,Stringstr,AttributeSetattr)throwsBadLocationException{if(str==null){return;}if(iMaxLength<=0)// aceitara qualquer no. de caracteres {super.insertString(offset,str,(javax.swing.text.AttributeSet)attr);return;}intilen=(getLength()+str.length());if(ilen<=iMaxLength)// se o comprimento final for menor... {super.insertString(offset,str,(javax.swing.text.AttributeSet)attr);// ...aceita str }}}
Chamo o metodo desta meneira:
importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.ItemEvent;importjava.awt.event.ItemListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.util.Vector;importjavax.swing.*;importjavax.swing.text.Document;importnegocio.fachadaTipo;publicclassGuiMoedaextendsjavax.swing.JFrame{/** Creates new form GuiMoeda */// Variables declaration - do not modify privatejavax.swing.JTextFieldcodigo;// End of variables declaration publicGuiMoeda(){java.awt.GridBagConstraintsgridBagConstraints;codigo=newjavax.swing.JTextField();getContentPane().setLayout(newjava.awt.GridBagLayout());setTitle("Moedas");codigo.setDocument(newTamanhoMaximo(2));gridBagConstraints=newjava.awt.GridBagConstraints();gridBagConstraints.gridx=1;gridBagConstraints.gridy=1;gridBagConstraints.ipadx=50;gridBagConstraints.anchor=java.awt.GridBagConstraints.WEST;getContentPane().add(codigo,gridBagConstraints);setLocation(280,180);setResizable(false);pack();}// </editor-fold> /** * @param args * the command line arguments */publicstaticvoidmain(Stringargs[]){java.awt.EventQueue.invokeLater(newRunnable(){publicvoidrun(){newGuiMoeda().setVisible(true);}});}}
packagebr.com.codesoftwares.swing.text;importjavax.swing.text.AttributeSet;importjavax.swing.text.BadLocationException;importjavax.swing.text.PlainDocument;publicclassExtremeDocumentextendsPlainDocument{privatestaticfinallongserialVersionUID=1L;publicfinalstaticbyteNORMAL=0;publicfinalstaticbyteLOWERCASE=1;publicfinalstaticbyteUPPERCASE=2;privatebytecaseRestriction=NORMAL;privateintmaxLength=-1;publicExtremeDocument(){super();setMaxLength(maxLength);setCaseRestriction(ExtremeDocument.NORMAL);}publicintgetMaxLength(){returnmaxLength;}publicvoidsetMaxLength(intmaxLength){this.maxLength=maxLength;}publicbytegetCaseRestriction(){returncaseRestriction;}publicvoidsetCaseRestriction(bytecaseRestriction){if(caseRestriction==NORMAL||caseRestriction==UPPERCASE||caseRestriction==LOWERCASE){this.caseRestriction=caseRestriction;}else{try{thrownewException("Esta restri\u00E7\u00E3o "+caseRestriction+" n\u00E3o pode ser aplicada");}catch(Exceptione){e.printStackTrace();}}}privateStringajustCaseRestrictionString(Stringstr){if(getCaseRestriction()==UPPERCASE){returnstr.toUpperCase();}elseif(getCaseRestriction()==LOWERCASE){returnstr.toLowerCase();}returnstr;}privateintgetTotalLength(Stringstr){returngetLength()+str.length();}publicvoidinsertString(intoffset,Stringstr,AttributeSetattr)throwsBadLocationException{if(str==null)return;if(this.getMaxLength()<0||getTotalLength(str)<=this.maxLength){super.insertString(offset,ajustCaseRestrictionString(str),attr);}else{if(getLength()==this.getMaxLength()){// N�o faz nada o tamanho da string � igual ao permitido.return;}// Insere somente o tamanho permitido eliminando o que sobrou.StringnewStr=null;if(this.getMaxLength()<0)newStr=str.substring(0,str.length());elsenewStr=str.substring(0,(this.maxLength-getLength()));super.insertString(offset,ajustCaseRestrictionString(newStr),attr);}}}