1

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).

budiDino
  • 13,044
  • 8
  • 95
  • 91
Kmag
  • 13
  • 4

2 Answers2

3

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.

Athi
  • 527
  • 5
  • 10
2

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.

André Schild
  • 4,592
  • 5
  • 28
  • 42
  • Thank you for your response. I may be misunderstanding your use of "properties", but I had problems using Vaadin ObjectProprties due to the lists of objects within my main object. A user can enter an arbitrary number of "characteristics" per object and each characteristic has multiple fields to it (doubles and strings). I couldn't figure out how to make properties as needed in this context. – Kmag Jul 09 '14 at 19:27
  • With properties I mean just ordinary variables, or class members. As for the data types you need, this is up to you to use the correct data structures to store your data in it. For generic data a hashmap is often a good idea. But without further infos I can't recommend what structures to use. – André Schild Jul 10 '14 at 06:08
  • I believe the Vaadin version of UI.getInstance is UI.getCurrent. The scope of variables with Vaadin UI's gets a little more complicated. The code here https://github.com/rolandkrueger/vaadin-by-example/tree/master/en/architecture/UsingSessionAndUIScope has a sample working with the differences in scope of variables. I haven't fully worked through the problem yet, but your answer pointed me in the right direction. Thank you again. – Kmag Jul 11 '14 at 18:23