Question

This is a sample of the code that I am using to instantiate a JFrame:

public class TestFrame{
public static void main(String[] args){
    JFrame frame = new JFrame();
    Insets insets = frame.getInsets();
    frame.setSize(new Dimension(insets.right + insets.left + 400, insets.bottom + insets.top + 400));
    System.out.println(String.format("Top = %d \nBottom = %d \nLeft = %d \n Right
            = %d", insets.top, insets.bottom, insets.left, insets.right));

    frame.setResizable(false);
    frame.setVisible(true);
}
}

The frame is displaying fine but all of the insets seem to be equal to zero. I need to know the size of the title bar on top because I want a Content Pane of size 400x400 exactly.

I tested it on multiple platforms and the same thing happens on windows and on mac.

What am I doing wrong ?

Was it helpful?

Solution

  1. If you want your content pane to be precisely 400x400, then I would consider setting its preferredSize to 400x400 (although I don't like to force preferred size, on a content pane, it may be acceptable, in some cases).
  2. After setting that preferred size and before showing the frame, call pack() on the frame.

This is the cleanest and easiest solution, and you don't have to care about border insets.

OTHER TIPS

If contentPane should be exactly 400 x 400, then create a class that retuns this for its preferredSize and use it as the contentPane:

class MyContentPane extends JPanel {
   public static final int PREF_W = 400;
   public static final int PREF_H = 400;

   public Dimension getPreferredSize() {
     return new Dimension(PREF_W, PREF_H);
   }
}

or perhaps better:

class MyContentPane extends JPanel {
   public int prefW;
   public int prefH;

public MyContentPane(int prefW, int prefH) {
   this.prefW = prefW;
   this.prefH = prefH;
}

   public Dimension getPreferredSize() {
     return new Dimension(prefW, prefH);
   }
}

As mentioned by the other posters, you'll have to use pack() on the top level window and use layout managers.

Myself I prefer not to do this but to rather let the actual preferred sizes of various compoennts and the container's layout managers do all of this deciding for me.

You are misunderstanding what you are working with, so it will seem "wrong".

Insets are not the dimensions of the "stuff" outside of the content pane. They are similar to a "border" around the content pane, where drawing will not be allowed to occur. Unlike borders, insets do not overlap.

The size of the decoration around the window is not determined by the program. It is determined by a different program, that handles focus, and window navigation. That program is called the window manager. It is the window manager that handles the "title bar on top" and it's height, font, etc.

To get an "exact size" program, you need to ask the window manager for a frame-less window, which can be done in Java. It will not have a title bar on top, nor will it have resizing handles near the edges, the window command menu, the red "x" to close, or a means for moving the window across your desktop (although your program can reposition itself if written to do so).

An alternative is to request an exclusive "screen" in the proper dimensions and root pane window, which was sometimes done with games. It is a more restrictive way of doing things, and resolutions are limited to supported screen sizes.

In the event that you really wanted the content pane to be a particular size, and not the whole window to be a particular size, please look to the other excellent answers that deal with a content pane's preferred size.

  1. Make your ui in a panel.
  2. Set the preferred size of the panel.
  3. Add the panel to frame and call pack()
  • BorderLayout (pre_implemented in the JFrame API) to ignore Insets,

  • most of LayoutManager ignore Insets too, only required for AbsoluteLayout,

basic and common stuff

  • JFrame (and most of Containers) can returns Insets, Bounds, Size only

    a) from already visible container

    b) after call JFrame(e.i.)#pack()

  • then in most cases is setting for Insets, Bounds, Size, XxxSize more than contraproductive, because

    a) most of JComponents can returns own and proper PreferredSize to its parent,

  • but excluding

    a) Graphics(2D),

    b) for empty containers

    c) JScrollPane & JList / JTable, there is contraproductive to generating own code monstrum in the case that JScrollPane is resizable with its container

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top