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?


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?

  • 2
    It sounds like you're making the classic rookie mistake of tightly coupling your application to its GUI. I don't have time to go into more detail right now (it's 3:45 am here!) but I'm sure googling something like "how to decouple GUI from application, Java" will give you interesting results. – MetaFight Jan 9 '16 at 3:46

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.

  • Please, no signletons. – Basilevs Jan 10 '16 at 8:22
  • I'm not sure why singletons are so bad for something like this, but you are welcome to provide alternative approach. – Low Flying Pelican Jan 10 '16 at 10:17
  • The actual problem here is that the Presentation and Model layers are merged. Shoving your Model into a singleton is a hack. It'll help a bit, but the UI and Model will still be very very tightly coupled. – MetaFight Jan 11 '16 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.