Results 1 to 4 of 4
- 05-30-2011, 08:22 AM #1
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
Allow only numerical input on JTextField
I am using netbeans to create a GUI with swing/matisse. I am trying to create a Phone Number and zip code FormattedJtextFields. How do I make it so only numbers can be entered? I have been reading tutorials about mask formatter but I am creating the JTextFields with GUI tools, not from scratch so how can I use these features?
-
If using NetBeans, it can create the Formatter for your JFormattedTextField for you. Simply create the JFormattedTextField, then scroll through its properties til you get to formatterFactory, and click on the button with the ellipsis "..." on it. Then follow the instructions, and you're golden.
- 05-31-2011, 11:32 AM #3
you can use the restrictions in Jtextfield follow the code to enter only numbers in the JtextField..
class LimitedText extends InputVerify
{
private int limit;
private boolean toUppercase = false;
public LimitedText(int limit)
{
super();
this.limit = limit;
}
public LimitedText(int limit,boolean upper)
{
super();
this.limit = limit;
toUppercase = upper;
}
@Override
public void insertString(int offs,String str,AttributeSet a) throws BadLocationException
{
if (limit == 0 || getLength() + str.length() <= limit)
{
if ((getLength() + str.length()) <= limit)
{
if (toUppercase) str = str.toUpperCase();
super.insertString(offs, str, a);
}
}
}
}
class InputVerify extends PlainDocument
{
@Override
public void insertString(int offs,String str,AttributeSet a)throws BadLocationException
{
if(str==null)
return;
char [] before = str.toCharArray();
String after = "";
for(int i=0;i < before.length;i++)
{
if( ((before[i]>='a' && before[i]<='f') || (before[i]>='A' && before[i]<='F')) || ((before[i]>='0' && before[i]<='9') || (before[i]>='0' && before[i]<='9')) )
after += before[i];
}
super.insertString(offs,after,a);
}
}
-
Similar Threads
-
Dynamically input text JTextField. Is it possible?
By africanhacker in forum New To JavaReplies: 2Last Post: 03-21-2011, 04:23 PM -
JTextfield renderer/editor input map
By mine0926 in forum New To JavaReplies: 1Last Post: 10-19-2010, 06:26 AM -
How to make JTextField input only allowed numbers ?
By BluXit in forum New To JavaReplies: 7Last Post: 04-10-2010, 11:31 AM -
Convert Input from JTextField to int?
By jls7168 in forum New To JavaReplies: 7Last Post: 02-20-2009, 02:29 AM -
Constraining Input in JTextField
By kataya in forum AWT / SwingReplies: 1Last Post: 06-26-2008, 06:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks