PDA

View Full Version : سوال: تعیین فرمت برای یک تکس فیلد



mahdis123
جمعه 13 مرداد 1391, 16:51 عصر
سلام.

دوستان من میخوام برای یک تکس فیلد یه فرمت خاص مشخص کنم. یعنی میخوام فقط داخل یک تکس فیلد بشه فقط یکدونه حرف وارد کرد .... برا اینکار باید چه کار کنم؟؟!! میشه راهنمایی کنید.

فکر کنم باید از JFormattedTextField استفاده کنم! ولی چجوریش رو نمیدونم... راهنمایی کنین ممنون میشم.:قلب:

spiderman200700
جمعه 13 مرداد 1391, 19:15 عصر
با استفاده از KeyListener هم میتونی همه جوره TextField رو محدود کنی

mahdis123
جمعه 13 مرداد 1391, 20:17 عصر
میشه یکم بیشتر توضیح بدی درباره همین KeyListener ؟؟ :( که چجور برا این کار گفتم میشه ازش استفاده کرد ؟!

مرسی

spiderman200700
جمعه 13 مرداد 1391, 21:41 عصر
با 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);
}
}
});

mahdis123
شنبه 14 مرداد 1391, 00:10 صبح
آها ، متوجه شدم ممنون :X

javaphantom
شنبه 14 مرداد 1391, 18:18 عصر
با 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 باشه هم عدد بگیره هم کاراکتر و اولشم با صفر بتونه شروع بشه

spiderman200700
شنبه 14 مرداد 1391, 18:55 عصر
درسته. ممنون از شما.
البته من گفتم یکی از روش ها اینه. نه تنها روش.