نوشته شده توسط
spiderman200700
با add کردن یه KeyListener به JTextField میتونی ورودی و خروجی ها رو کنترل کنی.
مثلا مثال زیر برای اینه که توی JTextField فقط عدد وارد بشه:
textField.setBackground(Color.WHITE);
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
textField.setEditable(false);
if (e.getKeyChar() >= '0' && e.getKeyChar() <= '9') {
textField.setText(textField.getText() + e.getKeyChar());
}
if (e.getKeyCode() == 8) {
textField.setEditable(true);
}
}
});
این روش، روش اشتباهی می باشد. و به اضای هر کلید یک متدود از اول تا آخر صدا زده می شود
روش صحیح برای کنترل کردن اکثر کامپونت های javax.swing درست کردن یک مدل هست.
در این قسمت شما باید از طریق property setDocument می تونی این مشکل و خیلی از مشکل های دیگه مثل انیکه فقط عدد بگیره یا مثلا با صفر شرو بشه یا نشه بین یک بازه باشه مثل اگر عدد هست بین 100 تا 1000 باشه .
به این کد دقت کند
public class CMSTextFieldDocument extends PlainDocument {
private int length;
private boolean isNumeric;
private boolean canZeroInFirst = false;
public CMSTextFieldDocument() {
}
public CMSTextFieldDocument(int length, boolean isNumeric, boolean canZeroInFirst) {
this.length = length;
this.isNumeric = isNumeric;
this.canZeroInFirst = canZeroInFirst;
}
public boolean isCanZeroInFirst() {
return canZeroInFirst;
}
public void setCanZeroInFirst(boolean canZeroInFirst) {
this.canZeroInFirst = canZeroInFirst;
}
/**
* Constructs a plain text document. A default model using
* <code>GapContent</code> is constructed and set.
*/
public CMSTextFieldDocument(int length, boolean isNumeric) {
this.length = length;
this.isNumeric = isNumeric;
}
public void setLength(int length) {
this.length = length;
}
public void setNumeric(boolean numeric) {
isNumeric = numeric;
}
/**
* Inserts some content into the document.
* Inserting content causes a write lock to be held while the
* actual changes are taking place, followed by notification
* to the observers on the thread that grabbed the write lock.
* <p/>
* This method is thread safe, although most Swing methods
* are not. Please see
* <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
* to Use Threads</A> for more information.
*
* @param offs the starting offset >= 0
* @param str the string to insert; does nothing with null/empty strings
* @param a the attributes for the inserted content
* @throws javax.swing.text.BadLocationException
* the given insert position is not a valid
* position within the document
*/
@Override
public void insertString(int offs, String str, AttributeSet a) {
if ((offs == 0) && (str.equals(" "))) {
offs = 0;
return;
}
if (str.endsWith("ی")) {
str = "ي";
}
try {
int offset = this.getEndPosition().getOffset();
int l = this.getText(0, offset).length();
if (l > length) {
return;
}
// String text = this.getText(0, offs);
if (str.length() > this.length) return;
if (offs > this.length - 1) throw new BadLocationException(str, offs);
if (this.isNumeric) {
if (!canZeroInFirst) {
if (offset > 0 && this.getText(0, offset).charAt(0) == '0') {
super.remove(0, 1);
offs = 0;
// text.replaceFirst("0", "");
// return;
} else if (offs == 0 && l > 1) {
return;
}
}
Double.parseDouble(str);
}
super.insertString(offs, /*text+*/str, a);
} catch (BadLocationException
e) {
return;
} catch (NumberFormatException
e) {
return;
}
}
}
یکی از بزرگترین اشکالات روش قبلی اینکه به ازای هر textfield باید بری یک event بنویسی با این روش فقط کافی هست که jtextField.setDocument(new CMSTextFieldDocument(10,false,true) CMS
یعنی اینکه به طول 10 باشه هم عدد بگیره هم کاراکتر و اولشم با صفر بتونه شروع بشه