How to increase the Vaadin notification/warning time? I am using Vaadin Valo theme. Nothing from the Book of Vaadin Notifications page helps.
First note that the five types of Notification
have different delays by default.
Notification.Type.HUMANIZED_MESSAGE
Notification.Type.TRAY_NOTIFICATION
Notification.Type.WARNING_MESSAGE
Notification.Type.ERROR_MESSAGE
Notification.Type.ASSISTIVE_NOTIFICATION
See below for the source code of an example app demonstrating each type.
The Notification
class has accessors for the delay: getDelayMsec
and setDelayMsec
.
For example, to set a delay to seven seconds first we must convert to milliseconds.
notification.setDelayMsec( 7 * 1_000 );
Or better yet, replace "magic" numbers with use of the TimeUnit
converter. The converter produces only long
values, so we must cast to an int
to satisfy the Vaadin setter.
notification.setDelayMsec( ( int ) TimeUnit.SECONDS.toMillis( 7 ) );
Beware of one big change in behavior from the default. If setting a positive delay number on an ERROR_MESSAGE
, the user need not click to dismiss; the error message disappears after the delay expires after a move of the mouse pointer.
See this code in action in the following example app. Uncomment the line of code calling the setter.
Here is some Vaadin 7.5.3 code. This class is a complete Vaadin app. Just create a new Vaadin project and replace with this code.
When disabling/enabling the setter, you may need to restart your Vaadin app. (Or buy a license for JRebel to avoid restarts.)
package com.example.vaadinnotifs;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import java.util.concurrent.TimeUnit;
/**
* By Basil Bourque.
*
* Source code provided under MIT License.
* http://opensource.org/licenses/MIT
*/
@Theme ( "mytheme" )
@Widgetset ( "com.example.vaadinnotifs.MyAppWidgetset" )
@SuppressWarnings ( "serial" )
public class MyUI extends UI
{
@Override
protected void init ( VaadinRequest vaadinRequest ) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin( true );
layout.setSpacing( true );
setContent( layout );
layout.addComponent( this.makeButton( Notification.Type.HUMANIZED_MESSAGE ) );
layout.addComponent( this.makeButton( Notification.Type.TRAY_NOTIFICATION ) );
layout.addComponent( this.makeButton( Notification.Type.WARNING_MESSAGE ) );
layout.addComponent( this.makeButton( Notification.Type.ERROR_MESSAGE ) );
layout.addComponent( this.makeButton( Notification.Type.ASSISTIVE_NOTIFICATION ) );
}
@WebServlet ( urlPatterns = "/*" , name = "MyUIServlet" , asyncSupported = true )
@VaadinServletConfiguration ( ui = MyUI.class , productionMode = false )
public static class MyUIServlet extends VaadinServlet
{
}
private Button makeButton ( Notification.Type typeArg ) {
// Taking advantage of Java’s nifty Enum capability to generically produce each Button instance.
Button b = new Button( typeArg.name() );
b.setData( typeArg ); // "setData" and "getData" are accessors for a little hiding place for any object you wish to carry with a Vaadin widget.
b.addClickListener( new Button.ClickListener()
{
@Override
public void buttonClick ( ClickEvent event ) {
Notification.Type t = ( Notification.Type ) event.getButton().getData(); // Cast from Object type used by the widget’s hiding place to the `Notification.Type` type we know we used.
Notification notification = new Notification( "This is a Notification message." , t );
//notification.setDelayMsec( ( int ) TimeUnit.SECONDS.toMillis( 7 ) );
Integer delayMillis = notification.getDelayMsec();
String description = "Notification.Type: " + t.name() + " has delay of " + delayMillis + " ms.";
notification.setDescription( description );
notification.show( Page.getCurrent() );
}
} );
return b;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With