Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
First use the vaadin and have no idea about the AbstractComponentContainer
I am studying the Events with Listeners
public class TheButtons extends CustomComponent implements Button.ClickListener {
Button thebutton;
Button secondbutton;
/** Creates two buttons in given container. */
public TheButtons(AbstractComponentContainer container) {
thebutton = new Button ("Do not push this button");
thebutton.addListener(this);
container.addComponent(thebutton);
secondbutton = new Button ("I am a button too");
secondbutton.addListener(this);
container.addComponent (secondbutton);
}
/** Handle button click events from the two buttons. */
public void buttonClick (Button.ClickEvent event) {
if (event.getButton() == thebutton)
thebutton.setCaption("Do not push this button again");
else if (event.getButton() == secondbutton)
secondbutton.setCaption("I am not a number");
}
}
The next is my application
public class TheButtonsApplication extends Application {
private static final long serialVersionUID = 2255770134219573908L;
@Override
public void init(){
/** Do not know where this is used correctly */
AbstractComponentContainer ac=new CustomComponent();
Window mainWindow=new Window("WindowOpener Application");
mainWindow.addComponent(new TheButtons(ac));
setMainWindow(mainWindow);
}
}
I really do not understand how to use the AbstractComponentContainer
Hello and welcome to the community.
As a tip when posting a code, it is good to use the code formatting so your code is easier to read. You can use the code formatting button in the editor or you can wrap your code with markdown tags.
What comes to using CustomComponent, you need to call setCompositionRoot in the CustomComponent constructor (TheButtons). Anyway there's not really a point to extend CustomComponent if you then plan to provide the container (or do you actually mean a Layout) outside its implementation. In the Application class where you are creating a new CustomComponent the same issue is present. You have to set a composition root.
Johannes Häyry: Hello and welcome to the community.
As a tip when posting a code, it is good to use the code formatting so your code is easier to read. You can use the code formatting button in the editor or you can wrap your code with markdown tags.
What comes to using CustomComponent, you need to call setCompositionRoot in the CustomComponent constructor (TheButtons). Anyway there's not really a point to extend CustomComponent if you then plan to provide the container (or do you actually mean a Layout) outside its implementation. In the Application class where you are creating a new CustomComponent the same issue is present. You have to set a composition root.
Thank you very match!
Johannes Häyry: Hello and welcome to the community.
As a tip when posting a code, it is good to use the code formatting so your code is easier to read. You can use the code formatting button in the editor or you can wrap your code with markdown tags.
What comes to using CustomComponent, you need to call setCompositionRoot in the CustomComponent constructor (TheButtons). Anyway there's not really a point to extend CustomComponent if you then plan to provide the container (or do you actually mean a Layout) outside its implementation. In the Application class where you are creating a new CustomComponent the same issue is present. You have to set a composition root.
I am so happy that you can help me to solve the problem.
I have read Book of Vaadin and at '4.4. Handling Events with Listeners' I have a problem.The code is
public class TheButton implements Button.ClickListener {
Button thebutton;
/** Creates button into given container. */
public TheButton(AbstractComponentContainer container) {
thebutton = new Button ("Do not push this button");
thebutton.addListener(this);
container.addComponent(thebutton);
}
/** Handle button click events from the button. */
public void buttonClick (Button.ClickEvent event) {
thebutton.setCaption ("Do not push this button again");
}
}
Next is the application
public class TheButtonApplication extends Application {
private static final long serialVersionUID = 2255770134219573908L;
@Override
public void init(){
}
}
I was confused at how can I give a object what is the type of AbstractComponentContainer to the TheButton in TheButtonApplication.
Hi,
Window is an abstract component container, so after you create your main Window object you can pass it to the constructor of TheButton to create a button. Something like this:
@Override
public void init(){
Window mainWindow = new Window("My Window");
TheButton theButton = new TheButton(mainWindow);
setMainWindow(mainWindow);
}
It's really a question of preference, but I prefer to not have listeners do the addComponent call. For instance, this would be my application's init method:
@Override
public void init(){
Window mainWindow = new Window("My Window");
Button button = new Button ("Do not push this button",
new TheButtonListener());
mainWindow.addComponent(button);
setMainWindow(mainWindow);
}
...and then your TheButton class (which I renamed for the example) could simply implement the buttonClick method. Inside that method, you can call getButton() on the click event to get a reference to the button that was clicked.
Cheers,
Bobby
Thank you for your help and the method is very suitable for me.Thank you