{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreiaqxptn62p7vaxqsdpf4ytojjcqi54j53tzjwta54kp7sk4kwrgbi",
"uri": "at://did:plc:dxjzgxe7cvirxkwfjr2tjspt/app.bsky.feed.post/3mh2ufacz6yp2"
},
"path": "/t/resizing-canvas-in-a-swing-application/49436#post_1",
"publishedAt": "2026-03-14T13:22:48.000Z",
"site": "https://hub.jmonkeyengine.org",
"tags": [
"@Override"
],
"textContent": "Hello Community,\nI have an issue with resizing the JME canvas inside my Swing application.\nThe 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.\nThe 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.\n\nHere is the initialization code for reference:\n```\n\n\n private void initJME3() {\n app = new DesignerApp();\n app.setProjectPath(projectPath);\n app.setDesignerFile(designerFile);\n\n // Load document\n if (designerFile.exists() && designerFile.length() > 0) {\n try {\n app.setDocument(DesignerDocument.load(designerFile));\n } catch (IOException e) {\n System.err.println(\"Failed to load designer document: \" + e.getMessage());\n app.setDocument(new DesignerDocument(designerFile.getAbsolutePath()));\n }\n } else {\n app.setDocument(new DesignerDocument(designerFile.getAbsolutePath()));\n }\n\n // Forward the scripts tree refresh callback to the app\n if (scriptsTreeRefreshCallback != null) {\n app.setScriptsTreeRefreshCallback(scriptsTreeRefreshCallback);\n }\n\n // Forward the code file updated callback to the app\n if (codeFileUpdatedCallback != null) {\n app.setCodeFileUpdatedCallback(codeFileUpdatedCallback);\n }\n\n // Set callback so the app can notify us of changes\n app.setDesignerPanelCallback(new DesignerApp.DesignerPanelCallback() {\n @Override\n public void onSelectionChanged(DesignerEntity entity) {\n SwingUtilities.invokeLater(() -> updatePropertiesPanel(entity));\n }\n\n @Override\n public void onSceneChanged() {\n SwingUtilities.invokeLater(() -> refreshSceneTree());\n }\n });\n\n AppSettings settings = new AppSettings(true);\n settings.setWidth(800);\n settings.setHeight(600);\n settings.setSamples(4);\n settings.setVSync(true);\n settings.setFrameRate(60);\n settings.setGammaCorrection(false);\n\n app.setSettings(settings);\n app.setPauseOnLostFocus(false);\n app.setShowSettings(false);\n app.createCanvas();\n\n JmeCanvasContext ctx = (JmeCanvasContext) app.getContext();\n ctx.setSystemListener(app);\n canvas = ctx.getCanvas();\n // Let the canvas fill all available space - no fixed preferred size\n canvas.setMinimumSize(new Dimension(100, 100));\n\n // Insert canvas into the right side of the main split\n JSplitPane mainSplit = (JSplitPane) ((BorderLayout) getLayout()).getLayoutComponent(BorderLayout.CENTER);\n JPanel canvasPanel = (JPanel) mainSplit.getRightComponent();\n canvasPanel.add(canvas, BorderLayout.CENTER);\n\n // Resize JME3 viewport when the canvas is resized\n canvas.addComponentListener(new ComponentAdapter() {\n @Override\n public void componentResized(ComponentEvent e) {\n int w = canvas.getWidth();\n int h = canvas.getHeight();\n if (w > 0 && h > 0 && app != null) {\n app.enqueue(() -> {\n app.onCanvasResized(w, h);\n return null;\n });\n }\n }\n });\n\n app.startCanvas();\n\n\n\n```\nSo far I tried to modify the AppSettings (Width / Height), The canvas settings (setPreferredSize, setMinimumSize), the canvas listener code (on componentResized modified the width / height).\nI 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.\nThe canvas works just fine and resized just fine. It’s just this unwanted padding.\nIt’s not a deal breaker but it’s annoying\n\nWDYT?\nThanks in advance",
"title": "Resizing canvas in a Swing application"
}