简体   繁体   中英

how do i get a object that has been created from a jdialog class to be use in another jdialog class

i have created 3 classes TourAgency class, addTourAgency Jdialog class and addGuide Jdialog class

TourAgency class

public class TourAgency 
{

  ...

  public TourAgency() {
        this("Not set", "Not set");
    }
public TourAgency(String inAgencyName, String inAgencyLocation)
{
  setAgencyName(inAgencyName); 
  setAgencyLocation(inAgencyLocation); 
}

addTourAgency class (the code where the agency is created)

public class addTAJD extends JDialog{

    ...

    private TourAgency ta1;

    okBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae)
        {
                String name = nameTF.getText().trim();
                String location = locationTF.getText().trim();

                ta1 = new TourAgency(name, location);

                ...

                setVisible(false);
                JOptionPane.showMessageDialog(null, ta1.getAgencyName() + " is created");
        }
    });

addGuide class (the part of the code to check whether the agency is missing or not)

public class AddNGuideJD extends JDialog{

    private JLabel name, idNumber, salary, extra;
    private JTextField nameTF, idNumberTF, salaryTF, extraTF;
    private JButton okBtn, resetBtn, cancelBtn;

    private Guide guide;
    private TourAgency ta1;

    okBtn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                if(ta1 != null)
                {
                    ...
                }

                else
                {
                    JOptionPane.showMessageDialog(null, "Please add a Tour Agency to countinue");
                }
            }
        });

so my questions is when i created a tour agency in the addTourAgency class and i click on the on button from the addGuide class, why does it always pop out the message saying an agency is not created?((JOptionPane.showMessageDialog(null, "Please add a Tour Agency to countinue");). Did i miss something? how can i used the object that i have already created?

Use getters and setters.
In the JDialog where you create AgencyTour create a getAgencyTour function and In the next JDialog create a setAgencyTour function to pass the result from the previous function.
Also you can pass the created AgencyTour from addTAJD as a parameter to the constructor of AddNGuideJD

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