It seems like whenever I am trying to create a program I always wind up using the setLayout(null);
command in Java because I like to absolutely position whatever it is I'm putting onto my swing components. From what I read everyone keeps saying to use layout managers to simply the coding process, but how does it simplify it? What is the problem that arises with absolute positioning between platform systems?
Asked
Active
Viewed 273 times
1

Cœur
- 37,241
- 25
- 195
- 267

Bobby C. Robillard
- 157
- 12
-
4Related: [Why is it frowned upon to use a null layout in Swing?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Pshemo Jun 13 '16 at 22:50
-
Layout Managed vs Layout Manually. And even without window resizing, http://www.miglayout.com/ is IMO much simpler when you want to have something that looks good quickly. – zapl Jun 13 '16 at 23:18
-
Back in the day, the nice people at Sun Microsystems hoped that Java/Swing would become _the_ language for web page automation. That was before Javascript ate its lunch and stole its name. So, anyway, Swing was meant for creating applets, and applets were expected to run in "containers" whose size they could not control. LayoutManagers were key to making it all work. – Solomon Slow Jun 14 '16 at 00:32
-
4*"I like to absolutely position whatever it is I'm putting onto my swing components."* I challenge you to create [this GUI](http://stackoverflow.com/a/5630271/418556) with a `null` layout. Two 'must have' features are 1) resizable, and.. 2) Can have any PLAF. – Andrew Thompson Jun 14 '16 at 00:49
2 Answers
5
When you use a layout, invoking pack()
"Causes this Window
to be sized to fit the preferred size and layouts of its subcomponents." When you don't, you have to try to calculate the bounds yourself. If (when) you get it wrong, as shown in the somewhat contrived example below, users will blame you—and not without some justification. A related example regarding non-resizable containers is seen here.
import java.awt.EventQueue;
import java.awt.FontMetrics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
/**
* @see https://stackoverflow.com/a/37801762/230513
* @see https://stackoverflow.com/a/12532237/230513
*/
public class Evil {
private static final String S = "Tomorrow's winning lottery numbers: 42, ";
private final JLabel label = new JLabel(S + "3, 1, 4, 1, 5, 9");
private void display() {
JFrame f = new JFrame("Evil");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
FontMetrics fm = label.getFontMetrics(label.getFont());
int w = SwingUtilities.computeStringWidth(fm, S) + 8;
int h = fm.getHeight();
label.setBounds(0, 0, w, h);
f.add(label);
f.setSize(w, h * 3);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Evil().display();
}
});
}
}
1
To put it simply, they do the work whenever your window size changes, because of desktop size or users dragging the window borders around. If you position things absolutely, they will be messed up whenever size changes.

Silverclaw
- 1,316
- 2
- 15
- 28