Re: TextField error checking
From: - ions (negative_ions_0_at_hotmail.com)
Date: 12/23/03
- Previous message: TNGgroup: "Voting System"
- In reply to: Anthony Borla: "Re: TextField error checking"
- Next in thread: Todd Corley: "Re: TextField error checking"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 23 Dec 2003 04:04:15 -0800
"Anthony Borla" <ajborla@bigpond.com> wrote in message news:<fyxFb.61381$aT.46044@news-server.bigpond.net.au>...
> "- ions" <negative_ions_0@hotmail.com> wrote in message
> news:4799447d.0312210507.73cb102@posting.google.com...
> >
> > 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/tutorial/uiswing/components/formattedtextfiel
> 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
- Previous message: TNGgroup: "Voting System"
- In reply to: Anthony Borla: "Re: TextField error checking"
- Next in thread: Todd Corley: "Re: TextField error checking"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|