Render Lucide SVG icons in Java Swing with JSVG
Render bundled Lucide SVGs as Swing icons with JSVG. Apply theme colors and reuse the parsed document when you draw an icon at a different size.
RSIS draws one Lucide search icon in two theme colors. I bundle the SVG with the app and use JSVG to draw it through Swing’s
Iconinterface.
The example uses Java 17 and JSVG 2.2.0. RSIS bundles the Lucide 1.82.0 SVGs that appear in the app. RSIS version catalog · RSIS icon inventory
Ship one search SVG
When I switch palettes, the search field changes its background and text color. I use the secondary text color for the magnifier too. Lucide sets the search SVG’s stroke to currentColor. I replace that value before parsing, so the same SVG paths work in both themes. Bundled search.svg · SearchField.applyTheme
| Asset requirement | Choice |
|---|---|
| A static picture with one color and size | PNG loaded with ImageIcon |
| A stroke icon drawn at several sizes and theme colors | A bundled SVG wrapped in a Swing Icon |
| A restaurant photo loaded at runtime | A raster image and an image loader or cache |
I put search.svg under src/main/resources/icons/lucide/. With Gradle’s standard resources layout, the resulting JAR exposes the file at /icons/lucide/search.svg. Loading it from the classpath works from any working directory. I ship the copyright and license notices that apply to the bundled files. RSIS bundled notices · Lucide license
Connect JSVG to Swing Icon
Put the SVG at the resource path above and save this configuration as build.gradle. For a new directory, also save rootProject.name = 'lucide-demo' in settings.gradle. The application plugin supplies the run task and puts JSVG and the resources on the classpath. JSVG 2.2.0 release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
plugins {
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'com.github.weisj:jsvg:2.2.0'
}
application {
mainClass = 'LucideDemo'
}
Save this Java file as src/main/java/LucideDemo.java and run gradle run on a machine with a display. The window shows the light and dark samples side by side. Both use a standard Swing text field, so you can compare the magnifier against the two backgrounds. Type a query and press the magnifier button to display it below the field.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import com.github.weisj.jsvg.SVGDocument;
import com.github.weisj.jsvg.parser.LoaderContext;
import com.github.weisj.jsvg.parser.SVGLoader;
import com.github.weisj.jsvg.view.ViewBox;
import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import javax.swing.*;
public class LucideDemo {
public static void main(String[] args) throws IOException {
String svg;
try (InputStream in = Objects.requireNonNull(
LucideDemo.class.getResourceAsStream("/icons/lucide/search.svg"),
"Missing search.svg")) {
svg = new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
Icon light = icon(svg, "#505e55", 20);
Icon dark = icon(svg, "#b5c7bd", 20);
SwingUtilities.invokeLater(() -> show(light, dark));
}
private static void show(Icon light, Icon dark) {
JPanel samples = new JPanel(new GridLayout(1, 2));
samples.add(sample(light, new Color(0xffffff), new Color(0x162820)));
samples.add(sample(dark, new Color(0x17241e), new Color(0xeef4f0)));
JFrame frame = new JFrame("Lucide search icon");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(samples);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private static JPanel sample(Icon icon, Color background, Color foreground) {
JTextField query = new JTextField(9);
JLabel result = new JLabel(" ");
JButton search = new JButton(icon);
search.setContentAreaFilled(false);
search.setToolTipText("Search restaurants");
search.getAccessibleContext().setAccessibleName("Search restaurants");
search.addActionListener(e -> result.setText("Search: " + query.getText()));
query.getAccessibleContext().setAccessibleName("Restaurant query");
result.setForeground(foreground);
JPanel searchRow = new JPanel();
searchRow.setOpaque(false);
searchRow.add(query);
searchRow.add(search);
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(background);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(searchRow, BorderLayout.CENTER);
panel.add(result, BorderLayout.SOUTH);
return panel;
}
static Icon icon(String svg, String hex, int size) {
byte[] bytes = svg.replace("currentColor", hex).getBytes(StandardCharsets.UTF_8);
SVGDocument document = new SVGLoader().load(
new ByteArrayInputStream(bytes), null, LoaderContext.createDefault());
return new SvgIcon(Objects.requireNonNull(document), size);
}
private record SvgIcon(SVGDocument document, int size) implements Icon {
@Override public int getIconWidth() { return size; }
@Override public int getIconHeight() { return size; }
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D copy = (Graphics2D) g.create();
try {
copy.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
document.render(c, copy, new ViewBox(x, y, size, size));
} finally {
copy.dispose();
}
}
}
}
Swing uses getIconWidth() and getIconHeight() when it lays out the button. It passes the final paint position to paintIcon(). The icon adapter hands those coordinates and dimensions to JSVG as a ViewBox. The adapter copies the graphics context so it can turn on antialiasing just for the icon. On a scaled display, Swing passes a graphics context that already includes the scale transform. I keep the requested size in logical pixels. JDK 17 Icon documentation
Cache parsed documents by icon and color
The example parses two variants before opening the window. RSIS reads the bundled icon sources during startup, outside the event dispatch thread. When a component requests an icon, AppIcons looks up the parsed SVGDocument by glyph and RGB color. The wrapper supplies the size when Swing paints it. A 16-pixel search icon and a 20-pixel search icon share a document if they use the same stroke color.
RSIS keeps the parsed documents for the life of the app. Its palettes supply a fixed set of colors, so each icon needs only a few cache entries. A color picker can add an entry on every drag. I’d cap the cache in that case or pick an adapter that can apply a stroke color at draw time. AppIcons resource loading and cache
RSIS also changes the Lucide stroke width from 2 to 1.65 in SVG units to match the Figma icons. That string replacement works with the attribute formatting in the bundled files. SVGs supplied by users can set the stroke through CSS, inheritance, or a differently formatted attribute. Support those styles if the app accepts outside files. RSIS paint adapter
Name the icon’s action
I give the demo button the accessible name “Search restaurants” so it describes the action when the button receives focus. I use the same wording in the tooltip for users who hover over the icon.
In RSIS, the magnifier beside the search field is decorative. I put the accessible name on the field where users type. The detail screen’s Back button names its destination so users know it returns them to the results. The magnifier’s stroke comes from the secondary text color in the Swing palette mapped from Figma. SearchField accessible name · Detail back action
Figure 1. The light theme shows the Lucide magnifier beside an empty field, a filled field, and a disabled field.
Figure 2. The same search fields in the dark theme use a lighter stroke for the magnifier.
Verify both icon variants
Run these commands in the RSIS checkout. The component test renders each registered icon to an image and checks that it paints visible pixels. The gallery writes light.png and dark.png under build/component-gallery. Compare the empty, filled, and disabled search fields in the two files. Component gallery · Icon paint test
1
2
./gradlew test --tests org.example.rsis.ui.components.ComponentsTest
./gradlew componentGallery
- Check that the JAR includes the SVG and the accompanying notices.
- Check the stroke against light and dark backgrounds at your normal display scale and at a higher scale.
- Tab to each icon-only button and activate it with the keyboard.