Re: TextField error checking

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 12/22/03


Date: Mon, 22 Dec 2003 17:18:28 GMT


"- ions" <negative_ions_0@hotmail.com> wrote in message
news:4799447d.0312212314.e5a35b0@posting.google.com...
> HGA03630@nifty.ne.jp (hiwa) wrote in message
news:<6869384d.0312211700.26222d12@posting.google.com>...
> > negative_ions_0@hotmail.com (- ions) 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 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/tutorial/uiswing/components/textfield.html

I hope this helps.

Anthony Borla