A good way to access vaadin components from other classes?

Hi!

I know how to access Vaadin components from other classes, but it requires lot of Java coding and lots of arguments in constructors. Is there a simpler way to have this kind of access between fields in classes?

At this class, I’m going to import Start_Stop class and I want to access all the fields from it.

@Route("")
@PWA(name = "Vaadin Application",
        shortName = "Vaadin App",
        description = "This is an example Vaadin application.",
        enableInstallPrompt = true)
@CssImport("./styles/shared-styles.css")
@CssImport(value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field")
public class MainView extends AppLayout{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	@Autowired
	private String[] devices; // This will be loaded at start up

	
	@PostConstruct
	public void init() {
		// Bar and tabs
		Image img = new Image("https://i.imgur.com/GPpnszs.png", "Vaadin Logo");
        img.setHeight("44px");
        addToNavbar(new DrawerToggle(), img);
        Tabs tabs = new Tabs(new Tab("Start & Stop"), new Tab("Graf"), new Tab("Databas"), new Tab("Larm"), new Tab("Manual"));
        tabs.setOrientation(Tabs.Orientation.VERTICAL);
        addToDrawer(tabs);
        
        // Import the pages - Should I place the start_Stop object as constructor arguments for the other classes????
        Start_Stop start_Stop = new Start_Stop(devices);
        Graf graf = new Graf();
        Database database = new Database();
        Larm larm = new Larm();
@Data
public class Start_Stop {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String START_STOP_SER = "Start_Stop.ser";
	private VerticalLayout vertical;
	private TextField temperature1;
	private TextField temperature2;
	private TextField temperature3;
	private TextField temperature4;
	private Checkbox checkbox1;
	private Checkbox checkbox2;
	private Checkbox checkbox3;
	private Checkbox checkbox4;
	private Button startStop;

	public Start_Stop(String[] devices) {

If you make Start_Stop a Spring component with the @UIScope annotation, you should get the same instance wherever you @autowire it (for the same user on the same application tab). Note that you can’t put the same component into multiple places in the UI, meaning if Start_Stop is a Vaadin component, you can’t re-use it in multiple views and you should create new instances instead.

Could you please elaborate a bit on what you want to achieve?

Thomas Mattsson:
If you make Start_Stop a Spring component with the @UIScope annotation, you should get the same instance wherever you @autowire it (for the same user on the same application tab). Note that you can’t put the same component into multiple places in the UI, meaning if Start_Stop is a Vaadin component, you can’t re-use it in multiple views and you should create new instances instead.

Could you please elaborate a bit on what you want to achieve?

Thanks. But I don’t going to change the session/scope. Only change the UI components, but still stand on the same page.
So it will be one MainView with an AppLayout. Then different components depending on what tab I press.

Each tab have a content, that are a vertical layout. I need to have the ability to communicate between all these vertical layouts.

OK, I see. Passing components and data through the constructors is indeed the easiest way to achieve that. The child would have e.g. a reference to the parent MainView and call mainView.formSubmitted() and so forth.

A more correct approach would be to use an event bus, Spring has one that can be used to great effect with Vaadin apps. Any time an action happens on a view, fire an event, and then take action in any other view. This way each individual view doesn’t need to know anything about the receiving end, and all you inject into the views is the Spring event object.

Sharing data can be done through events too, as events are just java classes/objects.

Another way is to separate the model handling to a separate class; each view would then only need a reference to that class to access data. The model class could also implement a notifier interface for when data changes and views need to update themselves.