13

I can't figure out how to get the windows taskbar height dynamicaly to set my application fullscreen.
As you know, taskbar can be in four positions: bottom, top, left or right, so I'm wondering if it's possible to also know the current position to set the window bounds.

EDIT: Using Lukas link I tryied this:

GraphicsDevice myDevice;
Window myWindow;

try {
    myDevice.setFullScreenWindow(myWindow);
    ...
} finally {
    myDevice.setFullScreenWindow(null);
}

But I'm gettin a NullPointerException

mastaH
  • 1,234
  • 3
  • 15
  • 30

5 Answers5

26

It is possible to obtain the Windows taskbar height if necessary:

Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle winSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();

int taskBarHeight = scrnSize.height - winSize.height;
Community
  • 1
  • 1
necman
  • 261
  • 3
  • 3
7

When you create your JFrame. Make a call to the setExtendedState() method in the JFrame API

jFrame = new JFrame("TESTER");
jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

The MAXIMIZED_BOTH setting will set your window to fullscreen and automatically take into account the position of the Taskbar.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • This could be a solution, but I was hoping for a toggle-method to set the JFrame fullscreen and go back to "normal" size. – mastaH Jul 27 '11 at 13:33
  • You could set a default size like `jFrame.setSize(450,450)` and then call `jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);` when you press the maximize button on the top of the window it will toggle between the two sizes. – Hunter McMillen Jul 27 '11 at 13:36
  • is there a way to toggle between max and normal with a button? – mastaH Jul 27 '11 at 13:40
  • NOTE: this will not work when using a swing "LookAndFeel" decorated window. It will cover the taskbar, So in this case as a work around for the bug in Java one has to know where the taskbar is to size the window accordingly. Really annoying !!! – peterk Jun 26 '12 at 15:59
  • @peterk This is from almost a year ago – Hunter McMillen Jun 26 '12 at 16:18
3

You can use:

int taskbarheight = Toolkit.getDefaultToolkit().getScreenSize().height 
    - GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height();

or you can use, as well:

JFrame frame = new JFrame();
frame.setSize(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getSize();
Martín Schonaker
  • 7,273
  • 4
  • 32
  • 55
nghiatm
  • 31
  • 1
2

If you want your Application to run in full-screen mode, you can enter it by getting a suitable GraphicsDevice and using the setFullScreenWindow(Window)-method:

GraphicsDevice myDevice = GraphicsEnvironment.
   getLocalGraphicsEnvironment().getDefaultScreenDevice();
Window myWindow;

try {
    myDevice.setFullScreenWindow(myWindow);
    ...
} finally {
    myDevice.setFullScreenWindow(null);
}

For further (and more complete) information. see the Docs)

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
1

This is part of a program I wrote:

public enum Location {
    TOP, RIGHT, BOTTOM, LEFT;
}

private static final class Taskbar {
    public final Location location;
    public final int width, height;

    private Taskbar(Location location, int width, int height) {
        this.location = location;
        this.width = width;
        this.height = height;
    }

    public static Taskbar getTaskbar() {
        Rectangle other = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getMaximumWindowBounds();
        return new Taskbar(other.x != 0 ? Location.TOP
                : (other.y != 0 ? Location.LEFT
                        : (other.width == IFrame.width ? Location.BOTTOM
                                : Location.RIGHT)), IFrame.width
                - other.width, IFrame.height - other.height);
    }
}

Essentially, calling Taskbar.getTaskbar() will give a taskbar containing information on its location (TOP, RIGHT, BOTTOM, LEFT), its width, and its height.

hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50