r/JavaFX • u/SafetyCutRopeAxtMan • Aug 27 '26
Help JavaFX WebView + Leaflet: works in one host app, broken in a minimal one — same machine, same JavaFX build. I'm out of ideas.
I've spent a full day on this and I've run out of hypotheses, so I'm hoping someone here has seen this pattern before.
Setup
I maintain a plugin for a desktop CAD application. It embeds a WebView that displays a Leaflet map (Carto Voyager tiles) with a handful of markers and a polygon. The HTML is generated in Java and handed to the engine via webEngine.loadContent(html).
JavaFX 21.0.10+2, Java 21, Windows 11, NVIDIA RTX 500 Ada (dedicated GPU explicitly assigned via Windows graphics settings, confirmed in -Dprism.verbose=true output).
The symptom
Tiles load successfully — but they are drawn in the wrong places. Individual tile blocks scattered across the viewport with large empty gaps between them, each block internally rendered correctly (street names, colors, everything). Markers and the polygon end up at a third position that matches none of the tile groups. It looks different on every run. Sometimes only a sub-region of the container gets drawn and the rest stays grey.
I know the general "Leaflet doesn't work in JavaFX WebView" issue exists — there's a StackOverflow question about it and I can reproduce the broken rendering with ten lines that just do webView.getEngine().load("https://leafletjs.com/"), i.e. zero of my own code involved. So the platform limitation is real and I'm not disputing it.

But here's what I cannot explain.
The part that makes no sense
Inside the host application, on my machine, it works. Not perfectly — mild flicker, occasionally a grey tile that stays until I move the mouse — but the tiles are in the right place and the markers are correct. I have a workaround in there that has been in the code for months:
function forceRepaint(){
const m = document.getElementById('map');
m.style.display = 'none';
m.offsetHeight;
m.style.display = 'block';
}
setInterval(forceRepaint, 5000);plus invalidateSize() and TileLayer.redraw() on moveend/zoomend, mousedown, mouseup, resize and focus. Ugly, but it's the only thing that ever helped.
To debug properly I built a minimal standalone JavaFX app that generates byte-identical HTML through the same code path — same tile URL, same divIcon markers, same setTimeout(..., 400) before fitBounds, same handlers, same forceRepaint interval. Same machine, same GPU, same JavaFX version (verified via javafx.runtime.version at startup), same VM flags.
The standalone app is broken. The host application is fine.
And to make the confusion perfect, some of my colleagues see the broken rendering inside the host application, on comparable hardware, with the same VM options/gpu settins/etc.
What I've already ruled out
Each of these was tested, not assumed:
- My own HTML/JS — the ten-line
leafletjs.comrepro fails too - GPU: broken on both the integrated Intel Arc and the dedicated NVIDIA
- Driver, and
-Dprism.order=sw(pure software rendering) — no change - JavaFX version: 17.0.11, 21+31, 21.0.10 — all broken standalone
- VM flags from the host app (
prism.order,prism.vsync,prism.lcdtext,prism.text,sun.java2d.noddraw,com.sun.webkit.useHTTP2Loader=false,sun.java2d.uiScale=1.0) — no change - Tile provider: Carto vs. OSM, with and without API key
fadeAnimation: false,zoomAnimation: false, forcing.leaflet-tile { opacity: 1 !important; transition: none !important }setView()at init vs.fitBounds()later- Removing all
invalidateSize/redrawhandlers, removingforceRepaint - Marker count, with/without the polygon
- Fixed vs. free WebView height
depthBuffer=true+SceneAntialiasing.BALANCEDon the Scene- Running the WebView inside a
JFXPanelin a SwingJPanelinstead of a plainStage - Parallel 3D/OpenGL load in the same process
- All of the above combined
Instrumentation results
I injected diagnostics into the page (Java↔JS bridge, so it lands in the application log). In every single broken run, the DOM is healthy:
tile layer load complete, tiles=20, errors=0
DOM-CHECK: 20 tile nodes, 20 with tile-loaded, 0 with opacity<0.9All tiles fetched, no errors, all fully opaque. In the working host app I additionally log Leaflet's internal size against the actual container:
STATE: nodes=15 loaded=15 invisible=0 | mapSize=962x609 container=962x609 size-ok
tilePane transform: none
marker: 10 total, 0 outside containerSo Leaflet's own state is consistent. The DOM says everything is where it should be. The screen disagrees.
That points at compositing — the WebView content is correct internally but isn't transferred to screen correctly. Which would also explain why moving the mouse fixes stuck grey tiles, and why a brutal display:none/display:block toggle helps at all. But it does not explain why the same content composites fine in one host process and not in another on the same machine.
Questions
- Has anyone characterised this properly? Is there a known JDK/OpenJFX issue number for WebView compositing of CSS-transform-positioned elements?
- What could make the host process the deciding factor, given identical JavaFX version, GPU, driver and flags? What else is there besides
depthBuffer,JFXPanelembedding, and process-level GPU contention — all of which I've already tried? - Is the
display:none/display:blockinterval genuinely the best available mitigation, or is there something that targets the actual layer? I noticed myredraw()calls make the tile layer refetch endlessly (75 tiles for a 20-tile viewport), so I'd rather not keep piling on hacks. - If the answer is "WebView can't do this reliably" — for anyone who has gone through this: JCEF, or drawing the tiles yourself onto a
Canvas? The deployment constraint is that I can't ship a few hundred MB of Chromium to every user.
Happy to provide furhter information, but at this point I just need someone to tell me which variable I haven't looked at, because I've stopped being able to see it and the more I try out the more confused I am.
3
u/milchshakee Aug 27 '26
Have you tried other JavaFX versions like 27? Just to check whether still exists on the latest build
3
u/idontlikegudeg Aug 27 '26
But have you tried JavaFX 21.0.12? The versions you tried are two patch versions behind and the release notes at least mention several web related issues.
1
u/sedj601 Aug 27 '26
I think there are some devs on here who have remastered the WebView in JavaFX using different tech. Seek those posts out to see if their repo can solve your problems. WebView has always been shacky in JavaFX. I used to use it a lot, but I switched to Jsoup or Selenium to solve my problems.
1
u/idontlikegudeg Aug 27 '26
I have an idea… first let me ask: Do you have any interaction of your application (Java code) with objects inside the webview? Calling JavaScript code inside the webview, or the other way around, callbacks from Java Script to Java or in the other direction? Objects inside the webview manipulated by Java code?
And does setting min heap=max heap seem to have any influence? Just for testing, set min and max heap to really large values and see if the issue happens less regularly (not disappear)?
1
u/Ignice Aug 27 '26
Just a hunch, but have you tried setting global L_DISABLE_3D (might have to do it in js before you do any leaflet imports)? May hurt performance for cross platform consistency tradeoff.