External Publication
Visit Post

Resizing canvas in a Swing application

jMonkeyEngine Hub March 14, 2026
Source

Hello Community, I have an issue with resizing the JME canvas inside my Swing application. The canvas set to some size inside the hosting panel and when the user changes the layout (resize) the canvas is also resized proportionally as expected. The problem is that it doesn’t stretch to the full size of it’s hosting panel (see attached video clip for reference). It always has significant padding on the right / bottom.

Here is the initialization code for reference:



    private void initJME3() {
        app = new DesignerApp();
        app.setProjectPath(projectPath);
        app.setDesignerFile(designerFile);

        // Load document
        if (designerFile.exists() && designerFile.length() > 0) {
            try {
                app.setDocument(DesignerDocument.load(designerFile));
            } catch (IOException e) {
                System.err.println("Failed to load designer document: " + e.getMessage());
                app.setDocument(new DesignerDocument(designerFile.getAbsolutePath()));
            }
        } else {
            app.setDocument(new DesignerDocument(designerFile.getAbsolutePath()));
        }

        // Forward the scripts tree refresh callback to the app
        if (scriptsTreeRefreshCallback != null) {
            app.setScriptsTreeRefreshCallback(scriptsTreeRefreshCallback);
        }

        // Forward the code file updated callback to the app
        if (codeFileUpdatedCallback != null) {
            app.setCodeFileUpdatedCallback(codeFileUpdatedCallback);
        }

        // Set callback so the app can notify us of changes
        app.setDesignerPanelCallback(new DesignerApp.DesignerPanelCallback() {
            @Override
            public void onSelectionChanged(DesignerEntity entity) {
                SwingUtilities.invokeLater(() -> updatePropertiesPanel(entity));
            }

            @Override
            public void onSceneChanged() {
                SwingUtilities.invokeLater(() -> refreshSceneTree());
            }
        });

        AppSettings settings = new AppSettings(true);
        settings.setWidth(800);
        settings.setHeight(600);
        settings.setSamples(4);
        settings.setVSync(true);
        settings.setFrameRate(60);
        settings.setGammaCorrection(false);

        app.setSettings(settings);
        app.setPauseOnLostFocus(false);
        app.setShowSettings(false);
        app.createCanvas();

        JmeCanvasContext ctx = (JmeCanvasContext) app.getContext();
        ctx.setSystemListener(app);
        canvas = ctx.getCanvas();
        // Let the canvas fill all available space - no fixed preferred size
        canvas.setMinimumSize(new Dimension(100, 100));

        // Insert canvas into the right side of the main split
        JSplitPane mainSplit = (JSplitPane) ((BorderLayout) getLayout()).getLayoutComponent(BorderLayout.CENTER);
        JPanel canvasPanel = (JPanel) mainSplit.getRightComponent();
        canvasPanel.add(canvas, BorderLayout.CENTER);

        // Resize JME3 viewport when the canvas is resized
        canvas.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                int w = canvas.getWidth();
                int h = canvas.getHeight();
                if (w > 0 && h > 0 && app != null) {
                    app.enqueue(() -> {
                        app.onCanvasResized(w, h);
                        return null;
                    });
                }
            }
        });

        app.startCanvas();


So far I tried to modify the AppSettings (Width / Height), The canvas settings (setPreferredSize, setMinimumSize), the canvas listener code (on componentResized modified the width / height). I searched the forum and found some issues with the canvas inside Swing applications. Not sure what is the best approach for dealing with my issue. The canvas works just fine and resized just fine. It’s just this unwanted padding. It’s not a deal breaker but it’s annoying

WDYT? Thanks in advance

Discussion in the ATmosphere

Loading comments...