Java Swing How to - Align JLabel text vertically top








Question

We would like to know how to align JLabel text vertically top.

Answer

import javax.swing.JLabel;
import javax.swing.JOptionPane;
// www .j a  v a 2 s  .  c o  m
public class Main {
  public static void main(String[] args) {
    JLabel label = new JLabel("Username :", JLabel.RIGHT);
    label.setVerticalAlignment(JLabel.TOP);
    
    JOptionPane.showMessageDialog(null, label);
    
  }
}