Rhejinald
Rhejinald

Reputation: 57

Should I use a layout manager even if it seems unnecessary?

I have a JPanel, on it are added an image and label, in that order.

It is very unlikely that more will be added to the panel.

The default formatting looks fine when it's displayed, in spite of me not using a Layout Manager.

Is it best practice to have them laid out by a LM anyway to be safe for future development, or is it not worth the extra dev time making it look essentially the same?

Upvotes: 3

Views: 132

Answers (2)

Adamski
Adamski

Reputation: 54715

If you create a JPanel using the default constructor the panel will use a FlowLayout. If you're happy with the results of using this layout manager then by all means don't change anything.

If however you wish to explicitly not use a layout manager you need to set the layout to null; e.g.

JPanel pnl = new JPanel();
pnl.setLayout(null);

This isn't recommended as the results are then highly dependent on the particular Look & Feel used. More information here.

Upvotes: 9

nos
nos

Reputation: 229204

You're already using a layout manager. A JPanel has a FlowLayout manager by default

Upvotes: 6

Related Questions