PDA

View Full Version : تعیین maxlengthدرtextfield



ameysams
یک شنبه 17 تیر 1386, 06:54 صبح
با سلام خدمت دوستان عزیز در جاوا چگونه می توان maxlengthیک textfieldرا تععین کرد؟
با تشکر

zehs_sha
دوشنبه 18 تیر 1386, 10:05 صبح
کافی است که یک کلاسtextfield را ارث بری کنی و متد زیر را override کنید. و مقدار len را از طریق سازنده به داخل کلاس بفرستید.

protected void processKeyEvent(KeyEvent e){
//KEY_TYPED is key-down and key-up
//If the char is not numeric, substitute an illegal
// character so that the runtime system will reject
// it and beep.
if(e.getID() == KeyEvent.KEY_TYPED)
if (this.getText().length()>=len) e.setKeyChar('\000');

//always do this when overriding processXxEvent
super.processKeyEvent(e);
}//end processKeyEvent

/* public String getValue() {
return this.getText();
}*/