Updating Page Title on Navigation

There are two ways to update the page title during navigation:

  • using the @PageTitle annotation

  • implementing HasDynamicTitle

Note
These two approaches are mutually exclusive: using both on the same class will result in a runtime exception at startup.

Using the @PageTitle Annotation

The simplest way to update the Page Title is to use the @PageTitle annotation on your Component class.

Java

  1. @PageTitle("home")
  2. class HomeView extends Div {
  3. HomeView(){
  4. setText("This is the home view");
  5. }
  6. }
Note
The @PageTitle annotation is read from the actual navigation target only; neither its superclasses nor its parent views are considered.

Setting the Page Title Dynamically

Implementing the interface HasDynamicTitle allows us to change the title from Java at runtime:

Java

  1. @Route(value = "blog")
  2. class BlogPost extends Component
  3. implements HasDynamicTitle, HasUrlParameter<Long> {
  4. private String title = "";
  5. @Override
  6. public String getPageTitle() {
  7. return title;
  8. }
  9. @Override
  10. public void setParameter(BeforeEvent event,
  11. @OptionalParameter Long parameter) {
  12. if (parameter != null) {
  13. title = "Blog Post #" + parameter;
  14. } else {
  15. title = "Blog Home";
  16. }
  17. }
  18. }

See also: