Program for dynamically creating and adding JLabels

In this Java Swing tutorial, we will learn how to create JLabel components dynamically inside a JPanel based on the value entered in a JTextField. Whenever the user types a number, the application automatically creates that many labels and displays them in the panel without restarting the program.

For example:

  • If the user enters 3, three labels will be displayed.
  • If the user enters 5, five labels will be displayed.
  • If the text field is cleared, all labels are removed.




How It Works

To detect every change in the text field, we use the CaretListener interface. Whenever the user types or deletes a character, the caretUpdate() method is automatically called.

Inside this method:

  • The entered text is read from the JTextField.
  • All previously created labels are removed from the JPanel.
  • New JLabel objects are created according to the entered number.
  • The panel is refreshed using validate() and repaint().

SOURCE CODE
import java.awt.Color;
import javax.swing.*;
import javax.swing.event.*;

class CreateLabelsDynamically extends JFrame implements CaretListener {
 
 JTextField jtf;
 JPanel jpan;
 
 public CreateLabelsDynamically() {
  
  jtf = new JTextField(10);
  jpan = new JPanel();
  
  setLayout(null);
  
  jtf.setBounds(50, 20, 50, 30);
  jtf.addCaretListener(this);
  
  jpan.setBounds(50, 80, 200, 200);
  jpan.setOpaque(true);
  jpan.setBackground(Color.CYAN);
  
  add(jtf);
  add(jpan);
 }
 
 public void caretUpdate(CaretEvent ce)  {
  
  String str = jtf.getText();
  
  jpan.removeAll();
  
  if(!str.isEmpty() && str.matches("\\d+")) {
   int num = Integer.parseInt(str);
   
   for(int i = 0; i < num; i++) {
    jpan.add(new JLabel("Label " + i));
   }
  }
  jpan.validate();
  jpan.repaint();
 }
 
 public static void main(String args[]) {
  
  CreateLabelsDynamically c = new CreateLabelsDynamically();
  c.setTitle("My Frame");
  c.setSize(300,350);
  c.setVisible(true);
 }
}

Expected Output

After running the program, type a number inside the text field. The application immediately creates the corresponding number of labels inside the panel. Changing the number updates the labels dynamically.

OUTPUT 

C:\>javac CreateLabelsDynamically.java 
C:\>java CreateLabelsDynamically









Explanation of Important Methods

removeAll()

Removes all existing components from the JPanel before adding new labels. Without this method, old labels would remain visible.

validate()

Recalculates the layout of the panel after components are added or removed.

repaint()

Refreshes the panel so that the newly added labels become visible immediately.

Limitation

The current program assumes that the user enters only numeric values. If alphabetic characters or special symbols are entered, the application throws a NumberFormatException.

Keywords

  • Java Swing Dynamic JLabel
  • Create JLabel Dynamically
  • Java CaretListener Example
  • JPanel Example Java
  • Dynamic Components in Java Swing
  • JTextField Event Handling
  • Java Swing Tutorial

Conclusion

In this tutorial, we learned how to create Swing components dynamically using a JTextField, JPanel, JLabel, and CaretListener. This technique is useful for creating dynamic forms, questionnaires, configurable user interfaces, and data entry screens where the number of components is determined at runtime.


Comments

Popular posts from this blog

Develop an Android application to display “Hello World!” on screen

Build a Simple Android App to Display Student Details

Program to display Digits in 7-Segment Style Using Java