Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get effective screen size from Java

Tags:

java

swing

awt

I would like to get the effective screen size. That is: the size of the screen without the taskbar (or the equivalent on Linux/Mac).

I am currently using...

component.getGraphicsConfiguration().getBounds() 

...and subtracting the default taskbar size depending on the OS, but I would like a way that works even if the user has resized/moved the taskbar.

like image 681
Rasmus Faber Avatar asked Apr 12 '12 12:04

Rasmus Faber


People also ask

How do I find the size of my Java window?

The following Java code demonstrates how to get the screen size, using the Java Toolkit class: // java - get screen size using the Toolkit class Dimension screenSize = Toolkit. getDefaultToolkit(). getScreenSize();

How do you make a JFrame fit the screen?

Select all components of JFrame, right click, select 'Auto Resizing', check for both Horizontal and Vertical, close . Show activity on this post. Calling pack() will usually result in the window being resized to fit the contents' preferred size.


1 Answers

GraphicsEnvironment has a method which returns the maximum available size, accounting all taskbars etc. no matter where they are aligned:

GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds() 

Note: On multi-monitor systems, getMaximumWindowBounds() returns the bounds of the entire display area. To get the usable bounds of a single display, use GraphicsConfiguration.getBounds() and Toolkit.getScreenInsets() as shown in other answers.

like image 50
moeTi Avatar answered Sep 22 '22 03:09

moeTi