Java Program to change JLabel text after creation

Java 8Object Oriented ProgrammingProgramming

At first, set a text for JLabel −

JLabel label;
label = new JLabel("First Label");

Now change the above JLabel text using setText() −

// changing text
label.setText("Updated text");

Example

import java.awt.Font;
import javax.swing.*;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Label Example");
      JLabel label;
      label = new JLabel("First Label");
      label.setBounds(50, 50, 100, 30);
      label.setFont(new Font("Verdana", Font.PLAIN, 13));
      // changing text
      label.setText("Updated text");
      frame.add(label);
      frame.setSize(500,300);
      frame.setLayout(null);
      frame.setVisible(true);
   }
}

Output

raja
Published on 09-May-2019 08:43:41
Advertisements