Skip to content

Instantly share code, notes, and snippets.

View TatuLund's full-sized avatar

Tatu Lund TatuLund

View GitHub Profile
@TatuLund
TatuLund / GridSorter.java
Created January 17, 2023 09:20
Java API for vaadin-grid-sorter component
package org.vaadin.addons.tatu;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.data.provider.SortDirection;
import com.vaadin.flow.shared.Registration;
@TatuLund
TatuLund / TreeGridScrollingEnhancedPage.java
Last active April 23, 2024 10:19
Example for Vaadin 23 to workaround issues with scrollToIndex in TreeGrid. Note, in Vaadin 24 there is new hierarchical scrollToIndex method, and this example does not work with the latest Vaadin 24.
package org.vaadin.tatu;
import java.util.Collections;
import java.util.LinkedList;
import com.vaadin.flow.component.ClientCallable;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.component.treegrid.TreeGrid;
@TatuLund
TatuLund / GridDnDWithConversion.java
Created January 30, 2023 07:44
Example of Drag and Drop of items between two Grid's of different type object.
package org.vaadin.addons.tatu;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.Shortcuts;
import com.vaadin.flow.component.contextmenu.ContextMenu;
@TatuLund
TatuLund / PasteImage.java
Created February 9, 2023 07:06
Example of how to paste an image from clipboard to view using JavaScript in Vaadin
package org.vaadin.tatu;
import com.vaadin.flow.component.ClientCallable;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
@Route("paste")
public class PasteImage extends VerticalLayout {
@TatuLund
TatuLund / DatePickerView.java
Created February 22, 2023 08:24
Using key shortcuts is prone to race conditions in edge cases in Vaadin. Here is an example how to close DatePicker and postpone clicking of the button to the next round trip so that we get correct value.
package org.vaadin.tatu;
import java.time.LocalDate;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.KeyModifier;
import com.vaadin.flow.component.Shortcuts;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.html.Div;
@TatuLund
TatuLund / ChartView.java
Created February 24, 2023 11:42
Example of drag and drop data series to Chart with Vaadin
package org.vaadin.tatu;
import java.util.ArrayList;
import java.util.List;
import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.charts.Chart;
import com.vaadin.flow.component.charts.model.ChartType;
import com.vaadin.flow.component.charts.model.DataSeries;
import com.vaadin.flow.component.charts.model.Series;
@TatuLund
TatuLund / AsyncButtonView.java
Last active October 3, 2024 07:09
This is an example of how to reduce boiler plate code in Vaadin when using Push. This is a button that runs a given task in the background thread. When task is completes, its return value is given to update function, that is run in UI.access(). Button on is disabled on clicking it and re-enabled once the task is completed.
package org.vaadin.tatu;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@TatuLund
TatuLund / CheckboxesView.java
Created March 9, 2023 08:46
BeanCheckboxGroup is a CheckboxGroup that binds a bean as value. The bean type's properties will be introspected and CheckboxGroup will autopopulate with list of checkable options using property name. The properties of the value object are updated according to changes.
package org.vaadin.tatu;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.vaadin.flow.component.checkbox.CheckboxGroup;
import com.vaadin.flow.component.checkbox.CheckboxGroupVariant;
import com.vaadin.flow.component.customfield.CustomField;
@TatuLund
TatuLund / SingleClickButton.java
Last active February 6, 2024 06:28
Example of custom Button in Vaadin that adds listener for single clicks prior to Vaadin 24.1 which introduces proper single and double click listeners.
package org.vaadin.tatu;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.shared.Registration;
public class SingleClickButton extends Button {
public SingleClickButton(String caption) {
@TatuLund
TatuLund / AccessibleLoadingIndicator.java
Created April 17, 2023 06:12
Example of tweaking Vaadin's loading indicator to be announced by NVDA screen reader in Windows
package org.vaadin.tatu;
import com.vaadin.flow.component.UI;
public class AccessibleLoadingIndicator {
private static void loading() {
UI.getCurrent().getPage().executeJs(
"""
const indicator = document.getElementsByClassName('v-loading-indicator ')[0];