Java Swing How to - Change JLabel text after creation








Question

We would like to know how to change JLabel text after creation.

Answer

//from  ww  w  .j a v  a 2  s  .  c om
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Main {
  public static void main(String[] args) {
    
    JLabel label = new JLabel("java2s.com", JLabel.LEFT);
    label.setText("Centered");
    
    JOptionPane.showMessageDialog(null, label);
    
  }
}