'Trying to account for taskbar in Java results in inaccurate window size

When I try to run this code, it results in a JFrame that is slightly displaced to the right by around 10 pixels for some reason, and also the height of the window extends beyond the taskbar, when the desired effect is to make it finish AT the taskbar.

I've tried setting the jframe to Undecorated, which fixes the entire issue, everything ends up where it should be. But the second I set undecorated to false, it seems to displace and ruin the window's position.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Component extends JComponent
{


/**
 * 
 */
private static final long serialVersionUID = 1L;

public int width, height;
public int tps;

private Game game;

public Component()
{
    Toolkit kit = this.getToolkit();
    width = (int) kit.getScreenSize().getWidth();
    height = (int) GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getHeight();
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(width, height));

    game = new Game(this);
    add(game);

    initWindow("Test", this);
}

public void initWindow(String title, JComponent component)
{
    JFrame jf = new JFrame(title);
    jf.setResizable(false);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jf.add(component);
    jf.pack();
    jf.setLocationRelativeTo(null);

    jf.setVisible(true);
}

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source