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()
.
5 Answers
The Java 8 implementation of the Stage class provides a maximized property, which can be set as follows:
primaryStage.setMaximized(true);
-
2A small addition for the cases if the code above still doesn't work - call setMaximized() after calling show(). Jul 30, 2015 at 13:17
-
1@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 thenstage.setMaximized(true);
and after that I calledstage.show();
and that solved it. But anyways thanks for the hint! :)– KamiNov 11, 2015 at 15:02 -
@Kami :) Very odd practices, Kami, maybe the issue is in the OS environment? Mine was Ubuntu with the latest Oracle Java8 Nov 11, 2015 at 15:17
-
@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. :)– KamiNov 12, 2015 at 16:49
-
Hey, this is not an answer since this will not work if the stage's style is set to other values than DECORATED and UTIL– AminMay 4, 2016 at 13:28
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.
-
1I agree. This looks like an issue. But Thanks. this will work for now.– DorothyJul 30, 2011 at 14:07
-
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. Dec 16, 2012 at 8:58
-
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 :-) May 27, 2013 at 1:35 -
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. May 27, 2013 at 1:40
-
8This isn't technically maximized mode, either. It's just an unmaximized window that is the size of the screen.– crushFeb 19, 2014 at 21:28
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());
-
5As with another answer, this isn't maximized; it's just the same size as the screen.– GKFXApr 21, 2016 at 18:22
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
-
Combine this with the Multi-Screen compatible maximize logic to address the full screen issue. First
setMaximized
, then usegetVisualBounds
to set the X, Y, width and height of the stage. Apr 17, 2017 at 23:40
Use this to remove the Minimise, Maximise Buttons :
primaryStage.initStyle(StageStyle.UTILITY);
Where primaryStage is your Stage object.