General question abt window , view and layout

I have a general question and looking to get some example code out of this post.

Let’s say I have a main Window, I have some components in that Window. And a user comes in and clicks on a button say. I want to change the Window to another Window. or basically change the content of that window with something else

What’s the way to do this? I am assuming I have to create a new java file and add it in the init function?

I want to know how to break things down into separate java classes and use them in main application class.

Things like switching layout will help or not is another question I have, how do I use these layouts to switch content of Page if that’s possible

Navigation is done by changing the content of window, not by creating new application windows. In the simplest form you could have a class for each of your views extending some layout e.g. VerticalLayout. Then you could have your application class have methods like showReportView(), showReportDetailView() etc. In those methods you call Window.setContent with your view class. Those show methods could be then called from your view. This becomes quickly hard to maintain if you have a lot of different views, with complex logic. In that case you should use some of the popular patterns like Model-View-Controller, Model-View-Presenter, Model-View-ViewModel etc.

I am trying this and I don’t see anything on the main window. I was trying to understand what you mean by “Those show methods could be then called from your view.”

I have 3 files
MyView1.java
MyView2.java
Vaadinprj2Application.java

MyView1.java has

[font=Courier New]
package com.example.vaadinprj2;

import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

public class MyView1 extends VerticalLayout {

public MyView1() {
	
	Label lab1 = new Label("LABEL1");
}

}
[/font]

MyView2.java has
[font=Courier New]

package com.example.vaadinprj2;

import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

public class MyView2 extends VerticalLayout {

public MyView2() {
	
	Label lab2 = new Label("LABEL2"); 
}

}
[/font]

and finally, the application class has

[font=Courier New]
package com.example.vaadinprj2;

import java.awt.Menu;

import com.vaadin.Application;
import com.vaadin.ui.*;

public class Vaadinprj2Application extends Application {

private Window main = new Window("Main Window");

@Override
public void init() {

	 this.setMainWindow(main);
     showReportView();
     showReportDetailedView();
}

private void showReportView() {
	
	main.setContent(new MyView1());
}

private void showReportDetailedView() {
	
	main.setContent(new MyView2());
}

}
[/font]

Hi,
You you haven’t added the components to the views => they won’t be visible. Try the following:


public class MyView1 extends VerticalLayout {
  public MyView1() {
    Label lab1 = new Label("LABEL1");
    addComponent(lab1)
  }
}
  • similar for MyView2

Cheers,

Charles

Thank you. This worked as expected. I am seeing LABEL2. Which is the last view I want to show up.