简体   繁体   中英

How to access JFrame Input field Objects from Validation Class

I have created a GUI with the registration field (firstname, lastname, email etc.) and my GUI class is -

public class Registration extends JFrame {

    public static void main(String[] args) {
        try {
            Registration regform = new Registration ();
        } catch (Throwable e) {
            //
        }

      public Registration () {
            firstName = new JTextField();
            latName = new JTextField();
            btnRegistration = newJButton("Register");

            btnSubmit.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent arg0) {
                  //want to do something similar as below
                  //Validatefields Vf = new Validatefields(Registration Rg);
             }
            });

       }
}

Now I want to create a separate class to Validate all the fields on clicking on "Register" button. I am new with Java coding and I am not able to access all the input fields Object of Registration class from Validation Class.

please note Class Registration and Validatefields are under same package.

public Class Validatefields{
    public static void Validattion(Registration Rg){
      //here I want access the text field as below
      //String Name = "Object of Registration Class".firstName.getText();

        
        int validateFlag = 0;
        if(Name.equal("")){
          validateFlag = 1;
        }
        if(validateFlag==0){
            ApiCall APC = new ApiCall();
            APC.RequestAccessToken();
        }
   }
}

in the next steps I want to use another class to call API on successfull validation - so in this method I would be require to get all input fields value.

public Class ApiCall {
   public static void RequestAccessToken(){
      //Similarly I want to get the individual field value here and pass it in the API
   }
}

I tried to read similar example from different source from inte.net but not able to figure it out. any help would be really great.

Now I want to create a separate class to Validate all the fields on clicking on "Register" button.

This is a very good idea and you have the general approach. I think the main problem is that you don't quite have the correct syntax. But before we get to that, my suggestion is that you pass the fields to the constructor instead of the entire Registration object:

public static void Validattion(JTextField firstName, JTextField lastName){

Now you create an instance by passing the fields to the constructor:

Validatefields Vf = new Validatefields(firstName, lastName);

Note that you only use the variable names here, not the types. This is most likely the main problem that you encountered and caused an error in your attempt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM