2

I have code with null panel:

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

I used setBounds(x, y, width, heigth), for example here:

label2.setBounds(150, 220, 459, 311);

I read that this is not a good practice, can you tell me why?

Is it only because when you want to add something between one component and another you have to set them positions again and again or is it something else?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Persantarus
  • 37
  • 1
  • 6
  • 1
    How do you suspect it would look on a screen of a different resolution than the one you designed it on. Or what if say the window was resized? – DejaVuSansMono Oct 28 '16 at 19:17
  • Well, for the layout manager, a `LayoutManager` is used to manage your layout (I think that's pretty obvious). So, generally people prefer to use a `LayoutManager` to keep things simple (because all subcomponents will be placed in nice positions), but I'm not exactly sure if using a `null` layout causes any issues other than possible errors when trying to set the position manually. – hyper-neutrino Oct 28 '16 at 19:18
  • I forgot about: this.setResizable(false); this.setSize(600, 570); this.setLocationRelativeTo(null); I have this lines in my code. – Persantarus Oct 28 '16 at 19:18
  • @Persantarus And that is just a poor design. What if the end user wants to resize the window? You are just going to disallow this behavior and have a 600x570 block on the screen? – DejaVuSansMono Oct 28 '16 at 19:21

1 Answers1

5

There are multiple problems with absolute positioning:

  • Different screen sizes and resolutions, what looks great on your machine would come out differently on another screen with a different resolution.
  • In the same vein, what happens when a user resizes the screen because they want to run your application side by side with some other application (to copy paste or whatever)
  • Different locales and font sizes, what happens to your labels when you use another locale, or another font, or change the font size.

There's probably more reasons, but using a layout manager makes sure that content is redistributed when windows are resized, or when content of a container changes, ...

Using absolute positioning is probably the easiest way in the beginning, but it pays to get to know the different layout managers and how they operate. It will save you from a lot of headaches due to, for example, changing requirements.

  • True, and it applies to every graphic user interface, not only for java swing. – Gabriel Oct 28 '16 at 19:32
  • *"Using absolute positioning is probably the easiest way in the beginning.."* What do you mean by 'beginning'? Till the UI needs a **second component**? That's the point where I feel it becomes less useful than using layouts. Other reason(s) to add to the list: `null` layouts completely screw up scroll panes. Using layouts and `pack()` makes the UI the exact size it needs to be, without guessing or `magic numbers'. I'll end with my 'copy/paste' comment on them.. – Andrew Thompson Oct 29 '16 at 07:47
  • .. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 29 '16 at 07:47