6

I have several JPanels in a layout and whenever I change the size of the JFrame manually with my mouse, it stretches the JPanels and makes it look messy. Is there a way to disable this?

DannyD
  • 2,732
  • 16
  • 51
  • 73
  • Did you look this http://stackoverflow.com/questions/869245/how-to-locate-jlabels-to-an-absolute-position-on-java-gui ? – mbaydar Apr 14 '12 at 20:53

3 Answers3

10

JPanel is not resizable by the user.

If you are referring to JFrame, you can use

yourFrame.setResizable(false);
Shehzad
  • 2,870
  • 17
  • 21
4

It's all about the LayoutManager you are using. Some LayoutManagers like BorderLayout always give extra space to children added in the center position. However, north, south, east, and west only allocated the preferred size to them. My favorite LayoutManager is TableLayout which is extremely easy to use, and very powerful. You can choose how extra space is allocated to each child. So it really just depends on what you are using.

You can also setMaximumSize on the JFrame and it won't allow you to resize the JFrame.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
3

I would like to expand on the answers given by @Shehzad and @chubbsondubs.

First off, as Shehzad stated, "resizable" is not a known attribute of JPanel objects. It is, however, an attribute of the window or frame that contains the panel (and other components). The question is now how to prevent a panel from resizing when its container is resized. As chubbsondubs stated, it is all about the Layout Manager you are using. Consider the following code snippet:

  add(contextPanel, BorderLayout.CENTER);
  add(btnPanel, BorderLayout.SOUTH);
  add(sidebar, BorderLayout.WEST);

These lines were extracted from the constructor of a frame. The sidebar object is a panel using GridBagLayout to place text fields, labels, and other components in it. Even though I have set the preferredSize, minimumSize, and maximumSize attributes of the panel to the same dimensions, when I resize the dialog, the container doesn't "respect" the minimum, maximum, and preferred size attributes set in the sidebar object (Research Layout Managers to know why this is). Because my dialog (frame) doesn't respect these size constraints, I must isolate the sidebar from the parent frame. To do this, simply add the sidebar to another panel, then add this new panel to the dialog. In other words, the `sidebar' is added indirectly to the dialog by adding its immediate parent instead.

  JPanel dummyPane = new JPanel();
  dummyPane.add(sidePanel);

  add(contextPanel, BorderLayout.CENTER);
  add(btnPanel, BorderLayout.SOUTH);
  add(dummyPane, BorderLayout.WEST);

Now, in this case, the dummyPane "respects" the size constraints defined in the sidebar object while being resized by the parent frame.

UPDATE:

One last thing... If you are using Absolute Positioning, you must set the size attribute and not preferredSize, minimumSize, ormaximumSize. These are ignored when absolute positioning is used. Absolute positioning is when the layout managers is set to null and the x, y coordinates of the component are explicitly set. The size of the component can be set by using the setSize(int width, int height) or the setBounds(int x, int y, int width, int height) methods.

hfontanez
  • 5,774
  • 2
  • 25
  • 37