Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
How to add SVG to Vaadin

How to add SVG to Vaadin

Posted on December 8, 2024 by Toma Velev

Vaadin is a popular Java framework for building Web Applications, so it is logical to have the ability to add SVG to the Vaadin Web App.

Method 1: Using Vaadin’s built-in SVG support

Vaadin provides a built-in way to display SVGs using the SVG class. Here’s an example:

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.server.VaadinRequest;

public class MyUI extends Div {
    public MyUI() {
        add(new Svg("path/to/your/svg/file.svg"));
    }
}

In this example, we’re creating a new Div component and adding an SVG to it using the Svg class.

Method 2: Using Vaadin’s HTML component

You can also use the HTML component to display an SVG. Here’s how:

import com.vaadin.flow.component.Html;

public class MyUI extends Div {
    public MyUI() {
        add(new Html("<span><svg><path d='M10 10 L20 20'/></svg></span>"));
    }
}

In this example, we’re creating a new HTML component and adding an SVG to it using HTML code. I’ve personally used this in a Theme Picker Component.

Method 3: Using Vaadin’s Polymer component

If you’re using Vaadin’s Polymer components, you can use the Polymer class to display an SVG. Here’s how:

import com.vaadin.flow.component.polymer.Polymer;

public class MyUI extends Div {
    public MyUI() {
        add(new Polymer("svg", "<path d='M10 10 L20 20'/></svg>"));
    }
}

In this example, we’re creating a new Polymer component and adding an SVG to it using HTML code.

Method 4: Using Vaadin’s Image component

You can also use the Image component to display an SVG. Here’s how:

import com.vaadin.flow.component.image.Image;

public class MyUI extends Div {
    public MyUI() {
        add(new Image("path/to/your/svg/file.svg"));
    }
}

In this example, we’re creating a new Image component and adding an SVG to it using the file path.

Method 5: Using Vaadin’s Build-in Icons (not entirely sure if are SVG)  

You can also use ResourcePath to display an SVG. Here’s how:

import com.vaadin.flow.component.html.Div;

public class MyUI extends Div {
    public MyUI() {
        add(VaadinIcon.ARROW_LEFT.create());
    }
}

In this example, we’re creating a new Div component and adding an SVG to it using the file path.

These are some of the ways you can add an SVG to Vaadin. The best method for your use case will depend on the specific requirements of your application.

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)