How to set a JavaFX Stage/Frame to Maximized
Asked Answered
D

5

44

I'm using JavaFX 2. I want my frame to open maximized but I'm not seeing a way. I searched a bit on the internet without success. For the stage I see setFullScreen() and setIconified() but I don't see anything like setMaximized().

Deangelis answered 28/7, 2011 at 19:27 Comment(0)
T
36

When evaluating the sourcecode of the Ensemble.jar provided with the samples of JavaFX 2.0 SDK, the currently valid way to achieve window maximizing is

Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();

primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());

(you find similar code in WindowButtons.java)

The 'maximize' button is still enabled and when clicking it, the windows will grow a bit more (Windows OS). After this the 'maximize 'button is disabled. In the provided example the standard buttons are replaced. Maybe this is still an issue.

Thomajan answered 30/7, 2011 at 11:56 Comment(6)
I agree. This looks like an issue. But Thanks. this will work for now.Deangelis
There is a feature request for that javafx-jira.kenai.com/browse/RT-10376 -- you may want to vote for it to get it done sooner rather then later.Keyte
While this seems the best we can do right now, there are many issues with this approach. For example it lacks the ability to reliably tell if a window is maximizes (so it can be saved/restored in session management) and it also doesn't work easily with multiple screens. You would have to iterate through Screen.getScreens() and find the one the window is on. JavaFX feels like Swing all over again in so many ways :-)Hostelry
PS: it's also not easy to undo the maximize given that it seems hard to track when it is happening (the only thing I can think of is to listen to both width and height changes and check if it seems to be the full size -- plus minus the bits that Windows likes to add. Buyer beware.Hostelry
This isn't technically maximized mode, either. It's just an unmaximized window that is the size of the screen.Ethiopia
Hi, This method was working earlier but it just stopped working. I haven't changed in Maximize Button code. But somehow it just stopped working.Monodrama
D
156

The Java 8 implementation of the Stage class provides a maximized property, which can be set as follows:

primaryStage.setMaximized(true);
Dysuria answered 15/12, 2013 at 23:51 Comment(7)
A small addition for the cases if the code above still doesn't work - call setMaximized() after calling show().Mardis
@AramParonikyan I had a problem with the code above. But calling show() alone, like you mentioned, doesn't solved the issue. I tried first to hide the stage with stage.hide(); and then stage.setMaximized(true); and after that I called stage.show(); and that solved it. But anyways thanks for the hint! :)Vasta
@Vasta :) Very odd practices, Kami, maybe the issue is in the OS environment? Mine was Ubuntu with the latest Oracle Java8Mardis
@AramParonikyan I'm using also Ubuntu and the Problem only occurs on ubuntu. JavaFX works on Windows without problems. Maybe the next update from Oracle could solve this. :)Vasta
Hey, this is not an answer since this will not work if the stage's style is set to other values than DECORATED and UTILStav
Best solution for java 8Veneaux
Works perfectly fine for me on Linux Mint XFCE (Java 17), even with an undecorated stage...Fibrilliform
T
36

When evaluating the sourcecode of the Ensemble.jar provided with the samples of JavaFX 2.0 SDK, the currently valid way to achieve window maximizing is

Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();

primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());

(you find similar code in WindowButtons.java)

The 'maximize' button is still enabled and when clicking it, the windows will grow a bit more (Windows OS). After this the 'maximize 'button is disabled. In the provided example the standard buttons are replaced. Maybe this is still an issue.

Thomajan answered 30/7, 2011 at 11:56 Comment(6)
I agree. This looks like an issue. But Thanks. this will work for now.Deangelis
There is a feature request for that javafx-jira.kenai.com/browse/RT-10376 -- you may want to vote for it to get it done sooner rather then later.Keyte
While this seems the best we can do right now, there are many issues with this approach. For example it lacks the ability to reliably tell if a window is maximizes (so it can be saved/restored in session management) and it also doesn't work easily with multiple screens. You would have to iterate through Screen.getScreens() and find the one the window is on. JavaFX feels like Swing all over again in so many ways :-)Hostelry
PS: it's also not easy to undo the maximize given that it seems hard to track when it is happening (the only thing I can think of is to listen to both width and height changes and check if it seems to be the full size -- plus minus the bits that Windows likes to add. Buyer beware.Hostelry
This isn't technically maximized mode, either. It's just an unmaximized window that is the size of the screen.Ethiopia
Hi, This method was working earlier but it just stopped working. I haven't changed in Maximize Button code. But somehow it just stopped working.Monodrama
P
8

try this simpler code primaryStage.setMaximized(true); and it fills up the whole screen . note that if you remove maximize/minise buttons the application will fill the whole screen as well as remove the taskbar so mnd your initStyles if you have any

Pyelitis answered 5/7, 2015 at 9:25 Comment(1)
Combine this with the Multi-Screen compatible maximize logic to address the full screen issue. First setMaximized, then use getVisualBounds to set the X, Y, width and height of the stage.Zuber
Q
7

Better use Multi-Screen compatible maximize logic:

// Get current screen of the stage      
ObservableList<Screen> screens = Screen.getScreensForRectangle(new Rectangle2D(stage.getX(), stage.getY(), stage.getWidth(), stage.getHeight()));

// Change stage properties
Rectangle2D bounds = screens.get(0).getVisualBounds();
stage.setX(bounds.getMinX());
stage.setY(bounds.getMinY());
stage.setWidth(bounds.getWidth());
stage.setHeight(bounds.getHeight());
Quinze answered 5/6, 2014 at 12:33 Comment(1)
As with another answer, this isn't maximized; it's just the same size as the screen.Cavorelievo
S
-7

Use this to remove the Minimise, Maximise Buttons :

primaryStage.initStyle(StageStyle.UTILITY);

Where primaryStage is your Stage object.

Shaynashayne answered 30/3, 2013 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.