Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GUI hides windows taskbar

I am using JFrame to create my GUI for a desktop application. The size of the GUI I am setting according to the resolution of the platform screen using this code.

this.setSize(this.getToolkit().getScreenSize());

The problem is that when I run the application the GUI covers all of the screen. The Windows task-bar is also hidden behind the GUI.

I want that whatever the size of the task-bar is, the task-bar should be visible in all conditions. How do I achieve that?

like image 884
Asghar Avatar asked Jul 22 '11 13:07

Asghar


4 Answers

Since you are using a JFrame you should just call:

jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

This takes into account the position of the taskbar.

like image 80
Hunter McMillen Avatar answered Oct 31 '22 20:10

Hunter McMillen


Is your task-bar set to auto-hide?

I just ran this test code on my Windows 7 machine.

import java.awt.Frame;
import javax.swing.*;

class TestFrameSize {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame("Test Screen Size");
                f.setAlwaysOnTop(true);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                System.out.println(f.getToolkit().getScreenSize());
                f.setExtendedState(Frame.MAXIMIZED_BOTH);
                f.setVisible(true);
                System.out.println(f.getSize());
            }
        });
    }
}

In fact, I ran it twice. Here is the output:

Task-bar configured to 'auto-hide'

java.awt.Dimension[width=1920,height=1080]
java.awt.Dimension[width=1928,height=1088]

(In which the frame seems to be 8 pixels taller & wider than the available screen space - odd.)

Task-bar not configured to 'auto-hide'

java.awt.Dimension[width=1920,height=1080]
java.awt.Dimension[width=1928,height=1048]

40 pixels shorter, and no longer covering the task-bar.

like image 22
Andrew Thompson Avatar answered Oct 31 '22 20:10

Andrew Thompson


I know this is old question but when i use jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); or the setState it displays it properly while displaying it for the first time. When I minimize it and again maximize it, it again covers the Task Bar. I am using Windows 7 and Java 1.7.

I found the solution here

f.setExtendedState(JFrame.MAXIMIZED_BOTH);  
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();  
f.setMaximizedBounds(env.getMaximumWindowBounds());  
f.setVisible(true);  

this should do the trick.

like image 5
W A K A L E Y Avatar answered Oct 31 '22 21:10

W A K A L E Y


you should be able to find the TaskbarHeight with a method

say getTaskbarHeight();

the minus that from

setFullScreen();

I found this example online

Setting screen size - taskBar

like image 4
sealz Avatar answered Oct 31 '22 21:10

sealz