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().

Neuron
  • 5,141
  • 5
  • 38
  • 59
Dorothy
  • 2,842
  • 10
  • 33
  • 46

5 Answers5

156

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

primaryStage.setMaximized(true);
Hans
  • 2,448
  • 2
  • 24
  • 30
  • 2
    A small addition for the cases if the code above still doesn't work - call setMaximized() after calling show(). – Aram Paronikyan Jul 30 '15 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 then `stage.setMaximized(true);` and after that I called `stage.show();` and that solved it. But anyways thanks for the hint! :) – Kami Nov 11 '15 at 15:02
  • @Kami :) Very odd practices, Kami, maybe the issue is in the OS environment? Mine was Ubuntu with the latest Oracle Java8 – Aram Paronikyan Nov 11 '15 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. :) – Kami Nov 12 '15 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 – Amin May 04 '16 at 13:28
  • Best solution for java 8 – I.Tyger Feb 28 '19 at 18:21
  • Works perfectly fine for me on Linux Mint XFCE (Java 17), even with an undecorated stage... – Manius Feb 04 '23 at 22:47
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.

pmoule
  • 4,322
  • 2
  • 34
  • 39
  • 1
    I agree. This looks like an issue. But Thanks. this will work for now. – Dorothy Jul 30 '11 at 14:07
  • There is a feature request for that http://javafx-jira.kenai.com/browse/RT-10376 -- you may want to vote for it to get it done sooner rather then later. – Sergey Grinev Dec 16 '12 at 08: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 :-) – Peter Becker May 27 '13 at 01: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. – Peter Becker May 27 '13 at 01:40
  • 8
    This isn't technically maximized mode, either. It's just an unmaximized window that is the size of the screen. – crush Feb 19 '14 at 21:28
  • 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. – Nikhil Mahajan Sep 30 '15 at 18:46
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

Flash
  • 1,105
  • 14
  • 16
  • 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. – Bjørn Vårdal Apr 17 '17 at 23:40
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());
Frank Roth
  • 6,191
  • 3
  • 25
  • 33
  • 5
    As with another answer, this isn't maximized; it's just the same size as the screen. – GKFX Apr 21 '16 at 18:22
-7

Use this to remove the Minimise, Maximise Buttons :

primaryStage.initStyle(StageStyle.UTILITY);

Where primaryStage is your Stage object.

Raj Wadhwa
  • 335
  • 2
  • 13