Search code examples
javaswingdimension

JPanel dimension auto changes


I have the following class:

public class Projector extends JPanel{
  public Projector(){
    this.setPreferredSize(new Dimension(1024, 704));
  }
}

If I print Projector's dimension inside the constructor, I have the right dimension, but when I print the dimension somewhere outside the constructor, I get as result (1018, 675)

any ideas why is this happening and how can I fix this?


Solution

  • This is because Java doesn't count the pixels used for the Window border that your OS draws, including where the Minimize, Maximize and Close buttons are. (That's why the difference is bigger on the Y axis) There's another method to get the pixels wasted because of this.