Main GUI(jDialog) - creates an instance of a class which has a HashMap (Lets call this HashMapClass) within this GUI. Able to access the methods to return the HashMap inside this class.
Secondary GUI(jDialog) - Wanting to access the same instance of the class which has the HashMap, not create a new instance inside this jDialog. (want to access the same HashMap with the same data inside)
This is what i've tried so far. This is a button from the Main GUI to open up the Secondary GUI.
Main GUI
private HashMapClass hashMapClassNo;
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
SecondaryGUI sec = new SecondaryGUI(null);
sec.setModal(true);
sec.setLocationRelativeTo(null);
sec.setVisible(true);
}
private ActionListener init(HashMapClass hashMapClass) {
hashMapClass = hashMapClassNo;
return this;
}
}.init(hashMapClassNo));
This compiles, but I still cannot access the methods using external method calls, or the HashMaps inside the SecondaryGUI.
How do i access the methods from one jDialog, into another?
You're adding a new method to an anonymous inner class, and that will never work. I'm not even sure what you're trying to achieve with the init()
method. Myself, if I want another class to have direct access to an object, I'd pass that object into the other class via a constructor parameter or a setter method.
eg,
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
SecondaryGUI sec = new SecondaryGUI(hashMapClassNo); // pass it in
sec.setModal(true);
sec.setLocationRelativeTo(null);
sec.setVisible(true);
}
};
A better solution is to use an MVC design pattern, but that will require more up-front work on your part, but is worth it if this is a large complex program and thus you absolutely need to create highly cohesive low-coupling testable classes.
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.