1

When creating an AWT Frame in a Java program running on MacOS, the maximize button in the frame's title bar maximises the window:

public static void main(String args[]) {
    new Frame();
}

When I create a Swing JFrame instance instead, the same button toggles the window into fullscreen mode, which is the standard behaviour on MacOS. The button even looks different visually when I hover over it:

public static void main(String args[]) {
    new JFrame();
}

Is there any way how I can replicate JFrame's "maximize to fullscreen" behaviour with AWT's Frame? I scanned the code of the entire JFrame class and wasn't able to identify any logic that would toggle the Frame's behaviour in such a way.

tyrondis
  • 3,364
  • 7
  • 32
  • 56
  • 1
    You may be able to leverage the `com.apple.eawt` methods mentioned [here](https://stackoverflow.com/a/30308671/230513), but I haven't tried. – trashgod Dec 27 '21 at 23:42
  • 2
    Why use AWT in this day & age? – Andrew Thompson Dec 28 '21 at 05:19
  • 1
    I want something that’s light-weight and as simple as possible as I only need a window with a canvas. Everything else will happen through Java2D and I don’t see why I need all the baggage of Swing for opening a simple window. – tyrondis Dec 28 '21 at 08:57
  • I tried using `com.apple.eawt` via reflection and I get the following `java.lang.IllegalAccessException`: `cannot access class com.apple.eawt.FullScreenUtilities (in module java.desktop) because module java.desktop does not export com.apple.eawt to unnamed module @57fffcd7` – tyrondis Dec 28 '21 at 13:07
  • 1
    @tyrondis: AFAIK, Java 8 was the last SDK with full support; Java 11 lost `QuitStrategy`, and Java 17 has none; for reference, the example links to the source; also consider `setUndecorated()`. – trashgod Dec 28 '21 at 23:04

1 Answers1

1

Try this code :

import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Window;

public class MyFrame extends Frame {

    MyFrame() {
        for (Window w : Window.getWindows()) {
            GraphicsEnvironment.getLocalGraphicsEnvironment()
                    .getDefaultScreenDevice().setFullScreenWindow(w);
        }
    }

    public static void main(String args[]) {
        MyFrame f = new MyFrame();
        f.setVisible(true);
    }

}
j.developer
  • 474
  • 2
  • 5
  • 15