Sunday, November 9, 2008

setLayout(null);

Somehow, I dont like to use the built in layout managers in Java.
So, when I started to write a little project lately, I've been using code blocks like this:
resetButton = new Button("Reset");
resetButton.setBounds(440, 400, 100, 40);
resetButton.addActionListener(this);
add(resetButton);
However, the last button I've added went fullscreen.
(Or full-background, since the others remained visible.)

I remember, two or three years ago I spent an hour debugging (with the same problem), to find out, I was missing the line:
setLayout(null);
It's so simple, so obvious.
Without this, the layout manager comes in, and messes up my properly placed components.
(Coordinated with magic numbers...)
Luckily, this time it only took five minutes to figure out, what's the problem.

I should make a T-shirt with this line, for the nights when creating a new project.
And for the mornings after the night before.

No comments: