Asghar
Asghar

Reputation: 2346

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?

Upvotes: 5

Views: 13304

Answers (8)

Jalal Sordo
Jalal Sordo

Reputation: 1685

This is what worked for me in Windows 10/Java 8:

setExtendedState(Frame.MAXIMIZED_BOTH);
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();  
setSize(env.getMaximumWindowBounds().getSize());

Upvotes: 0

W A K A L E Y
W A K A L E Y

Reputation: 867

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.

Upvotes: 5

Anthony Holland
Anthony Holland

Reputation: 160

It is quite a lot easier than all this! The taskbar exists in the insets of the screen. So to get the space you want to occupy just use the results of the following two calls:

Toolkit.getDefaultToolkit().getScreenInsets(f.getGraphicsConfiguration())

and

f.getGraphicsConfiguration().getBounds();

where f is a window on the same screen as the taskbar.

Do not use Toolkit.getDefaultToolkit().getScreenSize(); because it will give you the size of the first screen but not necessarily the screen you are on.

Try playing around with the below frame while you

  • move the window from one screen to another
  • drag the task-bar from one edge to another of each screen
  • resize the task-bar
  • make the task-bar autohide
  • adjust your screen resolution.

Each time you press the button it will faithfully print out everything you need to known about the task-bar's position.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TaskBarExplorer {
    public static void main(String[] args) {
        final Frame f = new JFrame("F");
        JButton b = new JButton("do");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                print("current device");
                print(f.getGraphicsConfiguration());

                GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
                print("");
                print("all devices");
                for (GraphicsDevice gd : gds) {
                    print(gd.getDefaultConfiguration());
                }
                print("");
            }
        });
        f.add(b);
        f.setSize(100, 100);
        f.setLocation(600, 400);
        f.setVisible(true);
    }
    private static void print(GraphicsConfiguration gc) {
        print("insets: " + Toolkit.getDefaultToolkit().getScreenInsets(gc));
        print("bounds: " + gc.getBounds());
    }
    private static void print(String s) {
        System.out.println(s);
    }

}

Some example output for the lazy (this is from one execution, button pressed after different adjustments):

current device
insets: java.awt.Insets[top=0,left=0,bottom=78,right=0]
bounds: java.awt.Rectangle[x=0,y=0,width=1280,height=1024]

all devices
insets: java.awt.Insets[top=0,left=0,bottom=78,right=0]
bounds: java.awt.Rectangle[x=0,y=0,width=1280,height=1024]
insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]

current device
insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]

all devices
insets: java.awt.Insets[top=0,left=0,bottom=78,right=0]
bounds: java.awt.Rectangle[x=0,y=0,width=1280,height=1024]
insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]

current device
insets: java.awt.Insets[top=0,left=0,bottom=0,right=58]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]

all devices
insets: java.awt.Insets[top=0,left=0,bottom=0,right=0]
bounds: java.awt.Rectangle[x=0,y=0,width=1280,height=1024]
insets: java.awt.Insets[top=0,left=0,bottom=0,right=58]
bounds: java.awt.Rectangle[x=1280,y=0,width=800,height=600]

Upvotes: 3

Andrew Thompson
Andrew Thompson

Reputation: 168845

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.

Upvotes: 5

sealz
sealz

Reputation: 5408

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

Upvotes: 4

Cyril D.
Cyril D.

Reputation: 363

Perhaps you shoud use GraphicsDevice.setFullScreenWindow() instead of setFullScreen(). It will maximize a window as opposed to using the screen fully in a "sort-of" non windowed mode.

Upvotes: 3

StanislavL
StanislavL

Reputation: 57421

What about ?

jFrame.setState(JFrame.MAXIMIZED_BOTH);

Upvotes: 2

Hunter McMillen
Hunter McMillen

Reputation: 61550

Since you are using a JFrame you should just call:

jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

This takes into account the position of the taskbar.

Upvotes: 7

Related Questions