简体   繁体   中英

How Do I Retrieve Class Fields Set in JFrame 1 from JFrame 2

Good-day,

I'm a newbie to Java programming and I'm currently taking a class on this. We're working on a project now where we were tasked with creating our own classes. Thus far I have been successful. My application features two JFrame forms ( JFrame1 and JFrame2 ). I'm using the NetBeans 7.3.1 IDE and JFrame1 is set as the main class.

When I run the application, JFrame1 opens and I use the following code to set the values of the fields in my class " company " using a button:

HERE'S A LINK TO MY COMPANY CLASS

public class JFrame1 extends javax.swing.JFrame
  {

    //Create company object and assign it to myCompany
    company myCompany = new company(null, null, null, null, null);

    private void btn_okActionPerformed(java.awt.event.ActionEvent evt)                                       
    {                                           
        // TODO add your handling code here:

        //Create variables to hold the user input from the form
        String name = txt_company_name.getText();
        String street = txt_address_street.getText();
        String city = txt_address_city.getText();
        String state = txt_address_state.getText();
        String zip = txt_address_zipcode.getText();

        //Assign the form data to the fields in the company class
        myCompany.set_company_name(name);
        myCompany.set_company_street(street);
        myCompany.set_company_city(city);
        myCompany.set_company_state(state);
        myCompany.set_company_zipcode(zip);

        //Display a friendly message informing user that input has been accepted
        //then hide this form and display JFrame2.
        JOptionPane.showMessageDialog(null, "Company data entered successfully, "
                + "application will now open.\nClick OK to proceed.", "THANK YOU!",
                JOptionPane.INFORMATION_MESSAGE);
        new JFrame2().setVisible(true);  //Create new instance of my JFrame2 form and make it visible
        this.setVisible(false); //Hide this form from view
    } 
  }

After setting the values in the " company " class, the form closes and opens my JFrame2 where there's a label called " lbl_company_name ". I'd like to set the text property of this label with the values of the fields of my " company " class previously set using JFrame1.

This is what I've tried thus far, and the label is changed to a series of nulls. I have a feeling that this is because I am using the "new" keyword to create a new object - and not the same one that has the data I need. Am I right?

public class JFrame2 extends javax.swing.JFrame
  {

    //Create company object and assign it to myCompany
    company myCompany = new company(null, null, null, null, null);

    public void showCompanyInfo()
      {
        // Define variables and assign to them fields from my Company class
        String name = myCompany.get_company_name();
        String street = myCompany.get_company_street();
        String city = myCompany.get_company_city();
        String state = myCompany.get_company_state();
        String zip = myCompany.get_company_zipcode();

        // Use the variables above to manipulate the display of a label
        lbl_company_name.setText(name + " | " + street + "," + city + "," + state + " " + zip);
      }

    /**
     * Creates new form JFrame2
     */
    public JFrame2()
      {
        initComponents();
        jPanel1.setVisible(false);
        showCompanyInfo();
      }
  }

Your assistance is highly appreciated, thanks.

Forget specific code solutions and instead you should focus on your basic problem:

  • You are giving each class it's own Company object (again the first letter of a class name should be upper case, regardless of what your instructor says), and changes made to one Company object will not be reflected in the other since they are completely separate unique objects.
  • To solve this problem, both classes should share the same Company object. This can be passed from one to the other via a constructor parameter or via a setter method, setCompany(Company myCompany) same as you would set any field.
  • The code details should be for you to figure out. Your main problem with handling the other folks answers is that you're copying code not ideas. Write your own code with my and their ideas, and you'll have smoother sailing and a better understanding.
  • As I've stated in comment, it's a bad design to spit out different JFrames at the user, and you will find few programs that do this. Instead swap views , usually JPanels that hold your complex GUI's, via a CardLayout .

As a very crude way I could come up quickly I would suggest the following. In your JFrame1 class add a getter:

public class JFrame1 extends javax.swing.JFrame
{

    //Create company object and assign it to myCompany
    company myCompany = new company(null, null, null, null, null);

    public company getCompany() {
        return myCompany; // return your myCompany object where everything is set from fields
    }

    //... rest of your code in JFrame1...

Then in your JFrame2, remove company field (you are creating a new instance there now - hence the all nulls!) and add a company parameter to your showCompanyInfo() method:

public class JFrame2 extends javax.swing.JFrame
{


    public void showCompanyInfo(company c)
    {
        // Define variables and assign to them fields from my Company class
        String name = c.get_company_name();
        String street = c.get_company_street();
        String city = c.get_company_city();
        String state = c.get_company_state();
        String zip = c.get_company_zipcode();

        // Use the variables above to manipulate the display of a label
        lbl_company_name.setText(name + " | " + street + "," + city + "," + state + " " + zip);
      }

    /**
     * Creates new form JFrame2
     */
    public JFrame2()
      {
        initComponents();
        jPanel1.setVisible(false);
        showCompanyInfo(jPanel1.getCompany());
      }
  }

As a side note, use camel-case for class names, ie company is a bad name for a class, Company is better. Also use camel-case for methods, snake-case (with underscore) is not going to play well with Java Beans concept, ie rename all those get_company_name() to getCompanyName() etc.

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