0

I am working on a gui system using swing and I am trying to follow the Model-View-Controller model. I am sort of confused a stuck when it comes to implementing my event system so that the view can communicate with the controller when things occur on the gui.

My gui is dynamic so that I have a side panel that contains other JPanels on it. One of them happen to be a LoginPanel which throws a LoginEvent when someone tries to login. Since I want my SidePanel to be dynamic, the SidePanel doesn't know that the LoginPanel exists, it is just drawing what it is told to draw.

When I start the program, the model and view are started separately and the controller gets passed an instance of both the model and the view.

Using the MVC system, how do I tell the LoginPanel that a controller (that implements LoginEventListener) wants to listen to it when the LoginPanel is dynamic on my gui and doesn't exist at all times?

Robin Green
  • 32,079
  • 16
  • 104
  • 187
user2249516
  • 281
  • 2
  • 5
  • 12
  • 1
    Uhm, and your question is? – home Nov 13 '13 at 17:09
  • Also its bad practice to use `String`s for passwords: http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords – vandale Nov 13 '13 at 17:16
  • @home My view has a side panel and the side panel has a login panel. The **LoginPanel** stores **LoginListeners**, and sends the event to those listeners when someone tries to login. I want the side panel to be dynamic so it should not know that the **LoginPanel** exists, so how would I tell the **LoginPanel** to add the controller as a listener if the **View** doesn't necessarily know that the **LoginPanel** exists? Maybe I am just thinking about the MVC system incorrectly, but it seems what I am doing is really flawed. – user2249516 Nov 13 '13 at 17:16

1 Answers1

0

There is a difference between existing and being visible. Simply make your LoginPanel exist at all times, but use setVisible to hide and show it. This would be the simplest solution.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • Do layout managers update automatically when you set a component to visible/not visible? For example if I use flow layout for something and hide a component, will the other components adjust to that component being invisible? (Sorry, can't test since I am on my phone now) – user2249516 Nov 13 '13 at 22:18
  • You might have to call `revalidate` or `validate` or something. When I used to work with Swing this wasn't very well-documented in the Javadocs if I remember correctly, but that was before StackOverflow.com existed - you should have no trouble finding out how it works now. – Robin Green Nov 14 '13 at 07:59