Repository navigation
Zoom the page
A page that is too small for a large monitor, or too large for a small one, needs a zoom. A text reader, a dashboard on a wall, and a toolbar for people who see poorly all ask for one. In lwjwae, Java sets the zoom of a window with one call. In this guide, a page has three buttons, and Java zooms the window in and out.

public class PageZoom {
public static void main(String[] args) {
try (Application application = Application.create()) {
Window window =
application.open(
WindowParameters.builder()
.title("Zoom")
.size(new WindowSize(520, 250))
.resource("zoom/index.html")
.build());
window.bind("zoom", step -> PageZoom.zoom(window, step));
window.show();
application.run();
}
}
/** Zooms the window by a step of a quarter, or back to 100%, and answers the new zoom. */
private static String zoom(Window window, String step) {
double zoom =
switch (step) {
case "in" -> window.zoom() + 0.25;
case "out" -> window.zoom() - 0.25;
default -> 1.0;
};
window.zoom(Math.clamp(zoom, Window.MINIMUM_ZOOM, Window.MAXIMUM_ZOOM));
return Double.toString(window.zoom());
}
}window.zoom(factor) takes a factor: 1 is 100%, 1.5 is 150%, and 0.75 is 75%. window.zoom()
reads the zoom that the engine shows now. A factor outside Window.MINIMUM_ZOOM, 0.25, and
Window.MAXIMUM_ZOOM, 5, throws IllegalArgumentException, and so does NaN, so clamp a value
that you compute, as above.
The page calls the binding like any other:
document.querySelector("#larger").addEventListener("click", () => window.zoom("in"));
document.querySelector("#smaller").addEventListener("click", () => window.zoom("out"));
document.querySelector("#reset").addEventListener("click", () => window.zoom("reset"));The whole page zooms, the way the zoom of a browser does: text, images, and layout grow together,
and borders and shadows with them. The page sees a smaller viewport in CSS pixels, so a responsive
layout reacts: in the picture, innerWidth is 693 pixels at 75%, 520 at 100%, and 346 at 150%
(Windows says 347: the engines round in different ways). A media query on the width, and resize
events, see the same.
The zoom belongs to the window, not to the page. It stays through navigations, links to other origins included, and a window opened later starts at 100%.
Only Java. A browser lets the user zoom with Ctrl and the wheel, and with Ctrl and the plus and
minus keys; WebView2 does that by default. lwjwae turns it off there, so that the zoom is the same
on every backend and a page can't end up at a zoom that Java didn't choose. To give the user keys,
put zoom entries with accelerators in a menu (MenuItem.of(...)), and call window.zoom from them.
| Platform | What zooms |
|---|---|
| Windows | The zoom factor of the web view |
| macOS | The page zoom of the web view, which needs macOS 11 or later |
| Linux, GTK 3 and GTK 4 | The zoom level of the web view |
- The page has no call for the zoom: there is no
lwjwae.zoom. A page asks Java through a binding, as above, when it wants the zoom changed. - Read
innerWidthfor the size of the viewport at a zoom. The engines round it in their own ways, by a pixel. - The zoom isn't remembered between runs. To keep it, save the factor in the store and set it again when the window opens.
Tutorial
- Set up the project
- Open a window
- Call Java from the page
- Send events from Java to the page
- Draw your own title bar
- Export notes through a native dialog
- Keep the application in the tray
- Show notifications
- Give the application an icon and a name
- Build a native executable
- Package the application
Guides
- Pass objects through the bridge
- Stream bytes between the page and Java
- Open several windows
- Place and control a window
- Make a transparent window
- Add menus
- Register global shortcuts
- Show progress in the taskbar
- Use the clipboard
- Handle dropped files
- Zoom the page
- Set the window icon
- Follow the theme of the desktop
- Use cookies
- Store data in SQLite
- Keep secrets
- Show dialogs
- Run a single instance
- Open your own links and files
- Update the application
- Handle page permissions
- Handle links and page loads
Reference