Java tutorial
//package com.java2s; import java.awt.Graphics; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class Main { public static void setLabelAboveOut(JLabel label, String str, int x, int y, int width) { Graphics g = label.getGraphics(); label.setText(str); int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str); int lX = x + width / 2 - strWidth / 2; int lY = y - g.getFont().getSize(); label.setBounds(lX, lY, strWidth, g.getFont().getSize()); } }