Indeed, you should not communicate in this way between 2 JFrame
.
Providing the full api of the component could create mix of responsibilities between the two JFrame.
Besides, using a static field to share data is a very bad idea as global variable generates often a strong coupling between components and processing that should not be and also increases the side effect risk when code using this variable is modified.
Only the behavior in terms of API should be offered to the user class.
You can create an interface to define the API of the JFrame
that you want to call from another JFrame
. Make the used JFrame
implement the interface and make the user JFrame
having a reference object to this interface.
The, you have just to communicate with the API from the user JFrame
.
API :
public interface UserLoginAPI{
String getLogin();
}
User JFrame
:
public class UserLoginFrame extends JFrame implements UserLoginAPI{
private String id;
...
public String getLogin(){
return id;
}
}
Used JFrame
:
public class OtherFrame extends JFrame {
private UserLoginAPI api;
private OtherFrame(UserLoginAPI userLoginAPI){
this.api = userLoginAPI;
}
public void doSomeProcessing(){
String loginRetrievedByApi = api.getLogin();
}
}