Take the 2-minute tour ×

I want to show many different labels over a map, so I'm using null layout in my panel, and calling setLocation for each label. For some reason, though, the labels don't show. If I remove the pan.setLayout(null), then the label appears in the top-center of the panel. Why isn't null layout working with setPosition?

package mapa;

import java.awt.*;
import javax.swing.*;

public class Mapa extends JFrame {
  private static JPanel pan;
  private static JLabel lab;

  public Mapa() {

  private static void createAndShowGUI() {
    Mapa frame = new Mapa();

    lab = new JLabel("TEXTO");

    pan = new JPanel();
    pan.setPreferredSize(new Dimension(640,480));
    lab.setLocation(100, 100);

    frame.getContentPane().add(pan, BorderLayout.CENTER);

  public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
share|improve this question
NULL LAYOUT IS EVIL –  Doorknob Jan 14 '13 at 0:40
add comment

2 Answers

up vote 4 down vote accepted

This is the problem with absolute positioning (or null layout). It requires you to set the sizes of all your components, otherwise they will stay are their default zero-size and won't appear. That's why it's always better to use a layout manager.

share|improve this answer
My labels are scattered all around the panel in specific positions (coordinates on the map), so I guess I have no option but null layout. I found out how to get the size of labels using getFontMetrics(), so set their size will be no problem. Thanks! –  Rodrigo Jan 14 '13 at 1:29
@Rodrigo no need for going as low-level as fontMetrics - even if you insist on doing the sizing manually: simply ask the label for its prefSize. But then: don't do the sizing/positioning manually, leave it to a suitable LayoutManager - which might be a highly specialized custom implementation which is aware of the coordinates which should control the location. Or do the locating yourself and at least leave the sizing to a manager, f.i. Rob's DragLayout –  kleopatra Jan 14 '13 at 12:51
To summarize what kleopatra said: Don't use label.getFontMetrics(), use label.getPreferredSize(). –  VGR Jan 14 '13 at 13:38
Thanks, GetPreferredSize worked fine too. Kleopatra, this "custom implementation" is not just what I did to put each label in its place? I read about DragLayout, but I don't need it to calculate the size of the panel, since its depending on the size of the panel that I calculate the labels' positions. –  Rodrigo Jan 15 '13 at 0:25
add comment

You have to set the size of the label explicitly; try using setBounds instead of setLocation. For example, lab.setBounds(100,100,200,30); Also there's no need to call setVisible(true); on the label.

Unless there's a very good reason to use a null layout and you know exactly what you're doing, using a layout manager is always where you should start.

share|improve this answer
add comment

Your Answer


