Access Methods on MainView by SecondView

Hello,
i’m using Java Vaadin-Framework and want to use a Method in my MainView from my SecondView.

The Structure is:

  • FooUI - declares an Navigator for the Views.
  • Login - First Window, started by Navigator Class
  • Settings - Setups some Settings, linked by Login Button over Navigator
  • MainView - This is my MainView with the Application!
  • SecondView - The Window where i pressing a Button and using Methods of MainView

My Problem is that if i accessing Methods of MainView, i have to Instantiate the MainView and then i generating a new MainView-Object what i dont want … because i want to use still the SAME INSTANCE of MainView. The reason is because the MainView was used by the User and i he should interact with it by the SecondView too …

I was reading about a Model-View-Presenter Architecture but i dont know how i can use it in my Project …

I tried to declare my MainView to a Singleton but i the problem is that if i call the .getInstance Method of MainView, i get a first-new Instance of the “Singleton” and not the Current Instance! …

Here is a Code-Snippet, if you need it:

public class ViewMain ... (
 aMethod(){
 ... do something on ViewMain GUI...
 }
}

public class SecondView ... {
ViewMain a = new ViewMain();
...
bMethod(){
a.aMethod(...)
 }
}

Thanks for Help!
max

You could add a setter and getter in your UI class for setting ViewMain like:

ViewMain main public void setViewMain(ViewMain main){this.main = main;} public ViewMain getViewMain(){return this.main;} Then you can set this variable in ViewMain like this:

getUI().setViewMain(this); and in SecondView you can use:

getUI().getViewMain().aMethod(); The UI is a Singleton itself so there is only one instance per user session.

ah nice thank you Marius!
Thats a really good idea,

For now, i just did a workaround, but next time i will do it on that way ! :slight_smile: