How to access components outside its defined view?

Hi,

I’ve defined a MyUI class that adds a view defined by the MainView class:

public class MyUI extends UI implements ViewChangeListener {
....
getUI().getNavigator().addView("secure", new MainView());
....
}

The MainView class defines several components, but how to access those in other classes within the application?

Regards,
Gerard

Hi,

If your View is a component container, eg. a layout, you can use its API normally (eg. VerticalLayout.getComponent(index) etc). Or you can write getter methods yourself, this of course has the advantage that you can choose descriptive names. I recommend you store references to the views in the UI to be able to access them easier:

public class MyUI extends UI implements ViewChangeListener {

    private final MainView mainView = new MainView();
    ...
    getUI().getNavigator().addView("secure", mainView);
    ...
    doSomethingWith(mainView.getSomeComponent());
}

Hi,

Thanks for your response, but I’m afraid I don’t quite understand it!

If for example the MainView adds a menubar CustomComponent to the view using:

addComponent(menubar); and I want to access this menubar from another class!

I still don’t see how your answer fits in here, sorry?!

Regards,
Gerard

Hi Gerard,

Usually, it’s not good practice to introduce direct dependencies between your Views unless absolutely needed. What is the underlying problem that you are trying to solve?

Hi,

Thanks for trying to help me solve this issue which I think is a very basic one!

Well, I’ve as MyUI:

[code]
import javax.servlet.annotation.WebServlet;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import biobank.view.ErrorView;
import biobank.view.LoginView;
import biobank.view.MainView;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Component;
import com.vaadin.ui.UI;
import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.ViewChangeListener;

@SuppressWarnings(“serial”)
@Theme(“mytheme”)
public class MyUI extends UI implements ViewChangeListener {

Navigator navigator;

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = MosaphenUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {

// Create a navigator to control the views
navigator = new Navigator(this, this);
navigator.addViewChangeListener(this);

    navigator.addView("", LoginView.class);
    if (SecurityUtils.getSubject().isAuthenticated()) {
        mv = new MainView();
        getUI().getNavigator().addView("secure", new MainView());
    }
    navigator.setErrorView(ErrorView.class);
}
....

[/code]MainView adds for example a menubar CustomComponent like:

public class MainView extends VerticalLayout implements View {

final private StatusBar statusbar = new StatusBar();
....
addComponent(statusbar);


}

The MainView consists of a menubar, mainpane and the statusbar. The mainpane contains a button, and a ClickEvent handler. Now when I click that button I want to update the statusbar. How can I refer to the statusbar component within this ClickEvent handler?

This is a simple example, it becomes even more challenging when one wants to update more than one component!

I can’t find any examples on the Vaadin site how to tackle this, so I suspect I do misunderstand some basic stuff here!

Regards,
Gerard

https://code.google.com/p/guava-libraries/wiki/EventBusExplained

Google Guava has ‘EventBus’. One object publishes, other objects subscribe.

For a real life example of using an Event Bus, you may want to look at how the refactored QuickTickets Dashboard works: https://github.com/vaadin/dashboard-demo

Hi,

Thanks for your suggestions, but that needs some study from my part, because this option is completely new to me!

Regards,
Gerard