Button.ClickEvent
) or keyboard shortcut(Button.ClickShortcut
). Listener (Button.ClickListener
) can handle this action fired by the button.There are various styles to define listener that listens button click. Following are some of the code snippets to use button declaration, click & listeners.
Button Creation
There are four types of constructors to create a button. For more information and syntax see API Doc
Button Handler
- public class JavaadinbuttonexampleApplication extends Application implements
- ClickListener {
- @Override
- public void init() {
- button.addListener(this);
- mainWindow.addComponent(button);
- setMainWindow(mainWindow);
- }
- public void buttonClick(ClickEvent event) {
- // TODO Auto-generated method stub
- }
- }
If there are more than one event then we can write the handlers as shown in above and the logic which distinguishes the button event can be written in buttonClick function
Listener with more than one handler
- public class JavaadinbuttonexampleApplication extends Application implements
- ClickListener {
- @Override
- public void init() {
- button1.addListener(this);
- button2.addListener(this);
- mainWindow.addComponent(button1);
- mainWindow.addComponent(button2);
- setMainWindow(mainWindow);
- }
- public void buttonClick(ClickEvent event) {
- if (event.getButton() == button1) {
- // TODO Auto-generated method stub
- getMainWindow().showNotification("This is Button1 event");
- } else if (event.getButton() == button2) {
- // TODO Auto-generated method stub
- getMainWindow().showNotification("This is Button1 event");
- }
- }
- }
Another way to handle is event is to take the event handler method as a parameter.
Listener using anonymous class
- public class JavaadinbuttonexampleApplication extends Application {
- Button button1;
- public void buttonClick(ClickEvent event) { // TODO Auto-generated method
- stub getMainWindow().showNotification("Button1 event"); } });
- mainWindow.addComponent(button1); setMainWindow(mainWindow);
- }
- }
If we want to perform action from keyboard, we need to add shortcut key to the button.
Performing action from keyboard
- public class JavaadinbuttonexampleApplication extends Application implements
- ClickListener {
- Button button1;
- @Override
- public void init() {
- button1.setClickShortcut(KeyCode.ENTER);
- button1.addListener(this);
- mainWindow.addComponent(button1);
- setMainWindow(mainWindow);
- }
- public void buttonClick(ClickEvent event) {
- if (event.getButton() == button1) {
- // TODO Auto-generated method stub
- getMainWindow().showNotification(
- "This is Button1 event performed from keyboard");
- }
- }
For more supported key codes and modifier codes go thorough following link
https://vaadin.com/book/-/page/advanced.shortcuts.html#advanced.shortcuts.keycodes
Vaadin Button CSS
To custom style the button add following snippet to your code
buttton1.addStyleName("style.css");
CSS Rules
.v-button
.v-button-my-stylename .my-stylename
.v-button-wrap
.v-button-caption
.v-button-borderless .v-button-caption
.v-button-my-stylename .my-stylename
.v-button-wrap
.v-button-caption
.v-button-borderless .v-button-caption
Other Stuff
- There is not direct option to set unique ID for Vaadin button but workaround could be setdata which will be inherited from Abstract component.
Button button = new Button("Click here");
Object object;
button.setData(object);
Object object;
button.setData(object);
Further Reading:
API Documentation for Vaadin Button
https://vaadin.com/api/com/vaadin/ui/Button.html
For demo and examples
http://demo.vaadin.com/sampler#Buttons
http://demo.vaadin.com/book-examples/book/#component.button.basic