The Artima Developer Community
Sponsored Link

Java Answers Forum
Registration form example

1 reply on 1 page. Most recent reply: Oct 15, 2012 6:24 AM by Bhuvaneswari V

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Nate Hultman

Posts: 8
Nickname: vtgunner14
Registered: May, 2004

Registration form example Posted: Jun 13, 2004 2:05 PM
Reply to this message Reply
Advertisement
I was wondering if someone could help me finish up this program that i am writing. I have a good bit done, but just need help with these methods. My code so far is at the bottom of the page.

checkName: This method should check that the name is not empty. If the name is empty, the label foreground for name should be set to red. If the name is not empty, the name label foreground should be set back to its original color.

checkAddress: This method should check that the length of the address is at least 4. If the length is less than 4, then the label foreground for address should be set to red. If the length is 4 or greater, then the address label foreground should be set back to its original color.

checkCity: This method should check that the city is not empty. If the city is empty, the city label foreground should be set to red. If the city is not empty, the city label foreground should be set back to its original color.

checkState: This method should check for a valid state of PA or NY. If the state is PA or NY, the state label foreground should be set back to its original color. If the state is NOT PA or NY, the state label foregrounds should be set to red. Make sure you begin the state as 2 spaces.

checkZip: The zip codes should fall between 10000 and 20000 inclusive. If the zip code is less than 10000 or greater than 20000, then the zip label foreground should be set to red. If the zip code is between 10000 and 20000, then the zip label should be set back to its original color. Make sure you begin the zip code as 5 zeroes.

checkPhone: This method should check that the first 3 digits of the phone are 412 or 702. If the first 3 digits of phone number are 412 or 702, then the phone label foreground should be set back to its original color. If the first 3 digits of the phone are NOT 412 or 724, then the phone label foreground should be set to red.

checkEmail: This method should check that an email address has been entered with an @ sign somewhere in it. If an @ is somewhere in the email, then the email label foreground should be set back to its original color. If there is no @ in the email address, then the email label foreground should be set to red. If you had called your String retrieved from the emailBox textfield as email, then you might say:

String email = emailBox.getText( ); // gets the text from the email box
if ( email.indexOf ( '@' ) == -1 ) // -1 means no occurence of @ sign in email
emailLabel.setForeground(Color.red);
else
emailLabel.setForeground(Color.white);


Once all of the methods have been executed and NO errors have occurred, a label saying "Thank You For Your Registration" should appear. If there are any errors in any of the items, then a label saying "Please correct items in red" should appear. You may adjust the actual words of these labels to be whatever you want.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Form extends Applet 
                         implements ActionListener {
  private TextField first = new TextField("first name",12);
  private TextField middle = new TextField("I",1);
  private TextField last = new TextField("last name",15);
  private TextField address = new TextField("address",25);
  private TextField city = new TextField("city",20);
  private TextField state = new TextField("state",2);
  private TextField zip = new TextField("zip",5);
  private Button submit = new Button("Submit");
  private Button reset = new Button("Reset");
  private TextArea message = new TextArea();
  
  public void init() {
    GridBagLayout gbl = new GridBagLayout();
    setLayout(gbl);
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.WEST;
    c.weightx=1.0;
    c.weighty=1.0;      
    c.fill=GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(5,5,5,5);
    gbl.setConstraints(first,c);      add(first);
    c.fill=GridBagConstraints.NONE;
    gbl.setConstraints(middle,c);     add(middle);
    c.fill=GridBagConstraints.HORIZONTAL; 
    gbl.setConstraints(last,c);       add(last);
    c.gridy=1; 
    gbl.setConstraints(address,c);    add(address);
    c.gridy=2; 
    gbl.setConstraints(city,c);       add(city);
    c.fill=GridBagConstraints.NONE;
    gbl.setConstraints(state,c);      add(state);
    c.gridy=3; 
    gbl.setConstraints(zip,c);        add(zip);
    c.gridy=4; 
    c.anchor = GridBagConstraints.CENTER;
    gbl.setConstraints(submit,c);     add(submit);
    gbl.setConstraints(reset,c);      add(reset); 
    c.gridy=5; 
    c.gridwidth=3;
    c.fill=GridBagConstraints.BOTH;
    gbl.setConstraints(message,c);    add(message);
    submit.addActionListener(this);
    reset.addActionListener(this);
  }
  public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if (source == submit){
       String initial = middle.getText();
       if (initial != null)
          initial += ". ";
       message.setText(first.getText() + ' ' + initial + last.getText());
       message.append('\n' + address.getText());
       message.append('\n' + city.getText() + ", " + state.getText());
       message.append(' ' + zip.getText());
    }
    else if (source == reset) {
       message.setText("");
       first.setText("");
       middle.setText("");
       last.setText("");
       address.setText("");
       city.setText("");
       state.setText("");
       zip.setText("");
    }
  }
}


Bhuvaneswari V

Posts: 1
Nickname: bhuvi
Registered: Oct, 2012

Re: Registration form example Posted: Oct 15, 2012 6:24 AM
Reply to this message Reply
hai how can i write the same registration form example using swings in java?

Flat View: This topic has 1 reply on 1 page
Topic: ClassLoader's reference in method area Previous Topic   Next Topic Topic: Hiding Rows

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use