Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable a JPanel so that it can't change size in Java

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?

like image 736
DannyD Avatar asked Apr 14 '12 20:04

DannyD


People also ask

How do you make a JPanel not resizable?

Making a Frame Non-Resizable: use setResizable(false) to freeze a frame's size. : JFrame Window « Swing « Java Tutorial. 14.80.

How do I make a JFrame not resizable?

Making a Frame Non-Resizable: use setResizable(false) to freeze a frame's size. 14.80.

How do you disable a component in Java?

I know you can use: component. setEnabled(false);

How do I remove the margins from a JPanel?

Following this answer you can put super(new FlowLayout ( FlowLayout. CENTER, 0, 0 )); at the top of your JPanel class. This will remove the padding between buttons within the class you are programming.


2 Answers

JPanel is not resizable by the user.

If you are referring to JFrame, you can use

yourFrame.setResizable(false);
like image 181
Shehzad Avatar answered Oct 20 '22 22:10

Shehzad


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.

like image 22
chubbsondubs Avatar answered Oct 21 '22 00:10

chubbsondubs