I can't store data in an object in one view and access it (with data in it) from another view

995 views Asked by At

I am trying to store a set of objects in an ArrayList and access them in a helper class. I am using Vaadin and a view navigator to navigate from one view that takes user input and navigate to another view that displays some of it on a graph. The views are helper classes. The ArrayList of these objects was outside of the helper classes and I have tried changing the modifiers of the ArrayList (I tried static and final) and I tried referencing the variable from inside the helper classes by using the general variable name as well as the OuterClass.variableName. The reason I am storing it as a variable and not in a database (as seems to be the norm for Vaadin CRUDs) is that my object has lists of other objects inside of it and I could not figure out how to get a flexible input on the UI or the right container for it (I haven't worked with Java in years and I am new to vaadin). I should add that there are no syntax errors, but the ArrayList is always empty.

In essence, the problem I am having is that I can't create an object in one helper class view (object made from user input), store it, and be able to read it from another helper class view (to display the data graphically).

2

There are 2 answers

3
André Schild On BEST ANSWER

You have a main class which extends the UI class. This one is where you could add properties to store your per-user/instance data structures.

You can access the UI class from almost anywhere in your vaadin application. Via UI.getInstance() (and the cast to your main class) you have access to everything inside this.

0
Athi On

Other then the answer above You can use session attributes of Vaadin UI.

UI.getCurrent().getSession().setAttribute("button", new Button("Button"));
Button b = (Button) UI.getCurrent().getSession().getAttribute("button");

Other possible answer are for e.g. events - an NavigateToViewEvent that sends data to the chosen view.