Question

I'm fairly new to Java and GUIs and am trying to work on a side project. I'm finding that as I continue to make more and more GUIs within eachother, I am continuously needing to access information that originated in the first GUI, from within deeper GUIs.

For example, I have a variable in the first GUI, after a user clicks on four or five sequential buttons (thus 4 or 5 GUIs later), I need to access said information.

So far I've just been passing this information into the initialization of the GUIs via their constructors, thus accessing it from there. Is this the "proper" way of accessing said information? Or is there a convention/more efficient way of doing so?

Example:

Within the first (main) GUI, I have a variable holding a username. After the user clicks on "Login" - then "Account Settings" then "Profile" - from within the "Profile" GUI, I need to access the variable "userName" originating in the main GUI. Currently I just pass this information into the constructors leading to Profile - is there a better way to do this? Perhaps a way to centralize all this information?

Était-ce utile?

La solution

No, it's bad idea to pass data from GUI to GUI via constructors, what you could do is to create a simple singleton service to hold your data which can be injected to the UI via the constructor.

Licencié sous: CC-BY-SA avec attribution
scroll top