By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,470 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,470 IT Pros & Developers. It's quick & easy.

TextField error checking

P: n/a
I have created a JComboBox with its Items as a list of "M" numbers ie.
M1,M2,M3.......throgh too M110 (thes are the messier objects, a
catolouge of deep sky objects) the user selects of of these and views
it aswell as infomation. The program also has a JTextFiels which
allows the user to enter the M number. The problem i have is checking
that what the user has entered is valid, that being an M followed by 1
- 110 Nothing else, i thought of checking it against the items in the
comboBox with itemAt() but i cudnt work out a way of looping through
them, and using that within the if() Expression.... please help.
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ne*************@hotmail.com (- ions) wrote in message news:<47*************************@posting.google.c om>...
I have created a JComboBox with its Items as a list of "M" numbers ie.
M1,M2,M3.......throgh too M110 (thes are the messier objects, a
catolouge of deep sky objects) the user selects of of these and views
it aswell as infomation. The program also has a JTextFiels which
allows the user to enter the M number. The problem i have is checking
that what the user has entered is valid, that being an M followed by 1
- 110 Nothing else, i thought of checking it against the items in the
comboBox with itemAt() but i cudnt work out a way of looping through
them, and using that within the if() Expression.... please help.

You can use InputVerifier extended class, or, more simply, you can do
the check in the ActionListener for the JTextField.
Jul 17 '05 #2

P: n/a
HG******@nifty.ne.jp (hiwa) wrote in message news:<68**************************@posting.google. com>...
ne*************@hotmail.com (- ions) wrote in message news:<47*************************@posting.google.c om>...
I have created a JComboBox with its Items as a list of "M" numbers ie.
M1,M2,M3.......throgh too M110 (thes are the messier objects, a
catolouge of deep sky objects) the user selects of of these and views
it aswell as infomation. The program also has a JTextFiels which
allows the user to enter the M number. The problem i have is checking
that what the user has entered is valid, that being an M followed by 1
- 110 Nothing else, i thought of checking it against the items in the
comboBox with itemAt() but i cudnt work out a way of looping through
them, and using that within the if() Expression.... please help.

You can use InputVerifier extended class, or, more simply, you can do
the check in the ActionListener for the JTextField.


Thats where my problem lies, im not sure how to acually do the
checking (its proving harder than i thought it would). It will happen
with the ActionEvent for a button. All i have at the moment is
checking that the string starts with M, which is far from what i
want...

else if( !input_selection.startsWith("M")) //Not exaclly dummyproof
{
display error dialog message here....
}
Jul 17 '05 #3

P: n/a

"- ions" <ne*************@hotmail.com> wrote in message
news:47*************************@posting.google.co m...

I have created a JComboBox with its Items as a list of "M"
numbers ie. M1,M2,M3.......throgh too M110 (thes are the
messier objects, a catolouge of deep sky objects) the user
selects of of these and views it aswell as infomation. The
program also has a JTextFiels which allows the user to enter
the M number. The problem i have is checking that what the
user has entered is valid, that being an M followed by 1 - 110
Nothing else, i thought of checking it against the items in the
comboBox with itemAt() but i cudnt work out a way of looping
through them, and using that within the if() Expression....
please help.


You basically want to know whether a typed in value is valid i.e. conforms
to the expected format.

Code below [suitably modified] could be used to perform format checking in
your JTextField's ActionListener [i..e. you accept input after ENTER
pressed, check it with the code below, and either accept or reject the
data].

Other approaches also exist - it all depends how much effort you wish to
apply. The following link may be of help:

http://java.sun.com/docs/books/tutor...mattedtextfiel
d.html

I hope this helps.

Anthony Borla

// -----------------------------------------------------
public class TestIsValidSkyObjectID
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("Usage: java TestIsValidSkyObjectID SkyObjectID");
System.exit(1);
}

System.out.println(args[0] + " is "
+ (isValidSkyObjectID(args[0]) ? " valid" : " not valid"));
}

public static boolean isValidSkyObjectID(String skyObjectID)
{
if (skyObjectID.charAt(0) != 'M')
return false;

short numericSuffix = 0;

try { numericSuffix = Short.parseShort(skyObjectID.substring(1)); }
catch (NumberFormatException e) {}

if (numericSuffix < 1 || numericSuffix > 110)
return false;

return true;
}
}

// ---------------------------------------------
Jul 17 '05 #4

P: n/a

"- ions" <ne*************@hotmail.com> wrote in message
news:47*************************@posting.google.co m...
HG******@nifty.ne.jp (hiwa) wrote in message

news:<68**************************@posting.google. com>...
ne*************@hotmail.com (- ions) wrote in message news:<47*************************@posting.google.c om>...

I have created a JComboBox with its Items as a list of
"M" numbers ie. M1,M2,M3.......throgh too M110
(thes are the messier objects, a catolouge of deep sky
objects) the user selects of of these and views it aswell
as infomation. The program also has a JTextFiels which
allows the user to enter the M number. The problem i
have is checking that what the user has entered is valid,
that being an M followed by 1 - 110 Nothing else, i
thought of checking it against the items in the comboBox
with itemAt() but i cudnt work out a way of looping through
them, and using that within the if() Expression.... please help.

You can use InputVerifier extended class, or, more simply,
you can do the check in the ActionListener for the
JTextField.


Thats where my problem lies, im not sure how to acually
do the checking (its proving harder than i thought it would).
It will happen with the ActionEvent for a button. All i have
at the moment is checking that the string starts with M, which
is far from what i want...

else if( !input_selection.startsWith("M"))
//Not exaclly dummyproof
{
display error dialog message here....
}


A code snippet using the validation routine I earlier posted:

...
static boolean isValidSkyObjectID(String skyObjectID)
{
...
}
...
public void actionPerformed(ActionEvent evt)
{
// Check contents of field after user has pressed ENTER
if (!isValidSkyObjectID(inputTextField.getText()))
{
// Warn user of input validation problem ...

// * Display a message dialog
JOptionPane.showMessageDialog(null,
"Invalid Sky Object - Must be M1 - M110",
"Data Validation Warning ",
JOptionPane.INFORMATION_MESSAGE);

// * Or, update a status field / area ...
statusField.setText("Invalid Sky Object - Must be M1 -
M110");
// ...
}

// Input ok, do work ...
// ...
}
...

You may care to check out the following tutorial for additional help:

http://java.sun.com/docs/books/tutor...textfield.html

I hope this helps.

Anthony Borla
Jul 17 '05 #5

P: n/a
Look into javax.swing.text.DocumentFilter.

It lets you do realtime editing to to changes in the document with out
worrying about firing yourself into an event loop.

Below will work for you, just add it like

yourTextField.getDocument().addDocumentFilter( new MFilter() );

Good Luck,
Todd



import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
public class MFilter extends DocumentFilter
{

public void insertString(DocumentFilter.FilterBypass fb,
int offset,
String string,
AttributeSet attr) throws
BadLocationException
{

fb.insertString(offset, string, attr);
if( !validate( fb.getDocument() ) )
{
fb.remove(offset,string.length());
}
}

public void remove(DocumentFilter.FilterBypass fb,
int offset,
int length) throws BadLocationException
{

String orig = fb.getDocument().getText(offset,length);
fb.remove(offset, length);
if( !validate( fb.getDocument() ) )
{
fb.insertString(offset,orig,null);
}
}

public void replace(DocumentFilter.FilterBypass fb,
int offset,
int length,
String text,
AttributeSet attrs) throws
BadLocationException
{
String orig = fb.getDocument().getText(offset,length);
text = text.toUpperCase();
fb.replace(offset, length, text, attrs);
if( !validate( fb.getDocument() ) )
{
if( orig.equals( "" ) )
{
fb.remove(offset,text.length());
}
else
{
fb.replace(offset, length, orig, attrs);
}
}
}
public boolean validate(Document doc )
{
boolean retVal = true;

String text = "";
try
{
text = doc.getText(0, doc.getLength() );
}
catch( Exception e )
{
}
int len = text.length();
if( len > 4 )
{
return( false );
}
if( len > 1 )
{
try
{
int val = Integer.valueOf( text.substring(1)
).intValue();
retVal = val > 0 && val <= 110;
}
catch( Exception exc )
{
retVal = false;
}
}
retVal = (text.charAt(0) == 'M' && retVal );
return retVal;
}
}
Jul 17 '05 #6

P: n/a
"Anthony Borla" <aj*****@bigpond.com> wrote in message news:<fy******************@news-server.bigpond.net.au>...
"- ions" <ne*************@hotmail.com> wrote in message
news:47*************************@posting.google.co m...

I have created a JComboBox with its Items as a list of "M"
numbers ie. M1,M2,M3.......throgh too M110 (thes are the
messier objects, a catolouge of deep sky objects) the user
selects of of these and views it aswell as infomation. The
program also has a JTextFiels which allows the user to enter
the M number. The problem i have is checking that what the
user has entered is valid, that being an M followed by 1 - 110
Nothing else, i thought of checking it against the items in the
comboBox with itemAt() but i cudnt work out a way of looping
through them, and using that within the if() Expression....
please help.


You basically want to know whether a typed in value is valid i.e. conforms
to the expected format.

Code below [suitably modified] could be used to perform format checking in
your JTextField's ActionListener [i..e. you accept input after ENTER
pressed, check it with the code below, and either accept or reject the
data].

Other approaches also exist - it all depends how much effort you wish to
apply. The following link may be of help:

http://java.sun.com/docs/books/tutor...mattedtextfiel
d.html

I hope this helps.

Anthony Borla

// -----------------------------------------------------
public class TestIsValidSkyObjectID
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("Usage: java TestIsValidSkyObjectID SkyObjectID");
System.exit(1);
}

System.out.println(args[0] + " is "
+ (isValidSkyObjectID(args[0]) ? " valid" : " not valid"));
}

public static boolean isValidSkyObjectID(String skyObjectID)
{
if (skyObjectID.charAt(0) != 'M')
return false;

short numericSuffix = 0;

try { numericSuffix = Short.parseShort(skyObjectID.substring(1)); }
catch (NumberFormatException e) {}

if (numericSuffix < 1 || numericSuffix > 110)
return false;

return true;
}
}

// ---------------------------------------------


Excellent, just what i wanted, didnt think there would of been much to it! Thanks
Jul 17 '05 #7

P: n/a
You want to create your own Document implementation and override the public
void insertString(int offset, String str, AttributeSet a) throws
BadLocationException method.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software

"- ions" <ne*************@hotmail.com> wrote in message
news:47*************************@posting.google.co m...
I have created a JComboBox with its Items as a list of "M" numbers ie.
M1,M2,M3.......throgh too M110 (thes are the messier objects, a
catolouge of deep sky objects) the user selects of of these and views
it aswell as infomation. The program also has a JTextFiels which
allows the user to enter the M number. The problem i have is checking
that what the user has entered is valid, that being an M followed by 1
- 110 Nothing else, i thought of checking it against the items in the
comboBox with itemAt() but i cudnt work out a way of looping through
them, and using that within the if() Expression.... please help.

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.