Skip to content

Instantly share code, notes, and snippets.

View TatuLund's full-sized avatar

Tatu Lund TatuLund

View GitHub Profile
@TatuLund
TatuLund / ComboBoxScrollIntoView.java
Last active June 25, 2025 05:29
In Vaadin 23/24 regular ComboBox does not support scroll selected item into view. But with some JavaScript workaround this is possible to do with ComboBoxLight add-on from Directory (https://vaadin.com/directory/component/light-combobox) as unlike ComboBox, ComboBoxLight does not lazy load and hence the target item is in the DOM always.
package com.example.application.views;
// See: https://github.com/vaadin/flow-components/issues/3470
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.vaadin.addons.componentfactory.ComboBoxLight;
import com.vaadin.flow.component.Html;
@TatuLund
TatuLund / CustomComponent.java
Created April 23, 2024 09:52
A Vaadin code example how to create web component with shadow root in Java.
package org.tatu.components;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Html;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.ShadowRoot;
// Example of slotting of child component:
// component.getElement().setAttribute("slot", "content");
// String html = "<div><slot name='content'></slot></div>";
@TatuLund
TatuLund / ValidatingComboView.java
Created March 21, 2024 09:36
In Vaadin 24 there is a feature of client side validation support for Binder. This is particularly important for some fields like DatePicker. However you can extend the functionality to many other fields if you so wish. For example if you want the ComboBox to show validation error when the input is not matching the suggestions, here is an example.
package com.example.application.views;
import java.util.concurrent.CopyOnWriteArrayList;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.Notification.Position;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.binder.Binder;
@TatuLund
TatuLund / CurrencyField.tsx
Created March 13, 2024 12:09
An example how to implement general use currency field component in Hilla/React with Cleave.js library.
import {
TextField,
TextFieldProps,
} from "@hilla/react-components/TextField.js";
import { TextField as TextFieldWebComponent } from "@vaadin/text-field/vaadin-text-field.js";
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
// Cleave.js is a library for formatting input text content when you are typing.
// It is used to format the input value as a currency in this component.
// Cleave.js is not included in the Hilla components, so it needs to be installed separately.
@TatuLund
TatuLund / MenuBarView.java
Created February 28, 2024 12:17
An example for Vaadin 23/24 how to with JavaScript create automatically closing menu
package com.example.application.views;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.menubar.MenuBar;
import com.vaadin.flow.component.menubar.MenuBarVariant;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.router.Route;
@Route(value = "menu-bar", layout = MainLayout.class)
@TatuLund
TatuLund / LazyComponentView.java
Last active October 2, 2024 11:40
This is an example of LazyComponent for Vaadin 14/23/24. If the content creation of the component takes long. For example data needs to be fetched from the back end and computed, or it is an image whose loading takes time, one may need to produce the component in background process. This example component wraps the process in the Future which cr…
/**
* This is an example of LazyComponent for Vaadin 14/23/24. If the content creation
* of the component takes long. For example data needs to be fetched from the back
* end and computed, or it is an image whose loading takes time, one may need to
* produce the component in background process. This example component wraps the
* process in the Future which creates the component and upon completion adds to
* view. @Push annotation needs to be present in AppShellConfigurator.
*/
package com.example.application.views;
@TatuLund
TatuLund / ProgressDialogPresenter.java
Last active November 11, 2024 06:54
Simple example showing ProgressBar in a Dialog with Vaadin 14/23/24.
package com.example.application.views;
import java.io.Serializable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
@TatuLund
TatuLund / PasswordView.java
Created January 23, 2024 07:05
In Vaadin 24 PasswordField there is no caps lock warning feature yet. This code shows how you can use JavaScript to show caps lock warning in LoginForm of Vaadin. This can be useful in your LoginView.
package com.example.application.views;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.router.Route;
@Route(value = "password", layout = MainLayout.class)
public class PasswordView extends Div {
String javaScript = """
@TatuLund
TatuLund / TooltipUtils.java
Created December 27, 2023 11:25
There is no API in Vaadin 24 yet for setting overlay classname of the tooltip. You can use the following JavaScript utility function with Vaadin 24.2.0 and nwer.
public class TooltipUtils {
public static void setTooltipClassName(Component comp, String className) {
comp.getElement().executeJs(
"""
if ($0.getElementsByTagName('vaadin-tooltip').length == 1) {
$0.getElementsByTagName('vaadin-tooltip')[0]._overlayElement.setAttribute('class',$1);
} else {
const tooltips = document.getElementsByTagName('vaadin-tooltip');
for (let i=0; i<tooltips.length; i++ ) {
@TatuLund
TatuLund / GridContextMenuView.java
Created December 13, 2023 14:47
Example of how to open GridContextMenu with the left click on the icon while opening it with the right click elsewhere.
package com.example.application.views;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Focusable;
import com.vaadin.flow.component.Key;