Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9121681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:55:03+00:00 2026-06-17T05:55:03+00:00

I created a JFrame, and it contains a JPanel. I created a number of

  • 0

I created a JFrame, and it contains a JPanel. I created a number of JLabels. I can add the JLabels to the JPanel, and display them correctly. But I want to implement them so as they displayed sequentially; a time delay between each JLabel to be displayed.

After searching the StackOverfLow, I tried some code, but it has no effect!. So How to use a timer to make components(Labels) displayed one after the other by setting a time delay.

I Don’t want a fix for my code particularly in the answer. Just show how to display any type of components in a delayed manner, each component displayed after a period of time. That is all. I provided my code to show my effort in trying to solve the problem.

First this is a subclass of JLabel to use: (No problems with it)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.JLabel;

public class DLabel extends JLabel
{
    Dimension size = new Dimension(70, 75);
    Font font = new Font(Font.SANS_SERIF, 12, 35);

    public DLabel(String t)
    {
        this.setPreferredSize(size);
        this.setBorder(BorderFactory.createBevelBorder(1, Color.white, Color.black));
        this.setVerticalAlignment(JLabel.CENTER);
        this.setHorizontalAlignment(JLabel.CENTER);
        this.setText(t);
        this.setFont(font);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        Color color1 = new Color(226, 218, 145);
        Color color2 = color1.brighter();
        int w = getWidth();
        int h = getHeight();
        GradientPaint gp = new GradientPaint(
                0, 0, color1, 0, h, color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
        super.paintComponent(g);
    }
}

The other class that use the DLabel class:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;

public class DelayedLabels extends JPanel
{

    static JFrame frame;
    Timer timer;   //timer to be used for dealy

    DLabel card_1;  //Defining the DLabels
    DLabel card_2;
    DLabel card_3;
    JLabel[] labelsArray;

    public DelayedLabels()
    {
        this.setLayout(null);

        card_1 = new DLabel("1");
        card_2 = new DLabel("2");
        card_3 = new DLabel("3");

        labelsArray = new DLabel[3]; //create the array

        createLabelsArray(); //add the Labels Objects to labelsArray
        setLabelsLocations(labelsArray); // set the locations of the Labels to be displayed on the JPanel
        addLabelsToPanel(labelsArray); //The adding of the Labels to the JPanel
    }

    private void createLabelsArray()
    {
        labelsArray[0] = card_1;
        labelsArray[1] = card_2;
        labelsArray[2] = card_3;
    }

    private void setLabelsLocations(JLabel[] labels)
    {
        int length = labels.length;
        int gap = 10;
        int counter = 10;
        for (int i = 0; i < length; i++)
        {
            labels[i].setBounds(170, counter, 60, 70);
            counter = counter + labels[i].getBounds().height + gap;

        }
    }

    private void addLabelsToPanel(JLabel[] labels)
    {
        for (int i = 0; i < labels.length; i++)
        {
            frame.revalidate();
            frame.repaint();
            this.add(labels[i]);
            timer = new Timer(1000, timerAction); //timer to use with 1000 milliseconds
            timer.start();
        }
    }
    private ActionListener timerAction = new ActionListener() //action to be invoked after each label added
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            frame.revalidate();
            frame.repaint();
        }
    };

    private static void createAndShowGUI()
    {
        frame = new JFrame();
        frame.setSize(600, 700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DelayedLabels demo = new DelayedLabels();
        demo.setOpaque(true);
        frame.add(demo);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:55:04+00:00Added an answer on June 17, 2026 at 5:55 am
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DelayedLabels extends JPanel {
    
        static JFrame frame;
        Timer timer;   //timer to be used for dealy
        JLabel card_1;  //Defining the JLabels
        JLabel card_2;
        JLabel card_3;
        JLabel[] labelsArray;
        ActionListener listener;
    
        public DelayedLabels() {
            listener = new ActionListener() {
    
                int i = 0;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    Component c = DelayedLabels.this.getTopLevelAncestor();
                    DelayedLabels.this.add(labelsArray[i++]);
                    c.validate();
                    c.repaint();
                    if (i==labelsArray.length) {
                        timer.stop();
                    }
                }
            };
            this.setLayout(new GridLayout(0, 1, 20, 20));
    
            card_1 = new JLabel("Label 1");
            card_2 = new JLabel("Label 2");
            card_3 = new JLabel("Label 3");
    
            labelsArray = new JLabel[3]; //create the array
    
            createLabelsArray(); //add the Labels Objects to labelsArray
            timer = new Timer(1000,listener);
            timer.start();
        }
    
        private void createLabelsArray() {
            labelsArray[0] = card_1;
            labelsArray[1] = card_2;
            labelsArray[2] = card_3;
        }
    
        private static void createAndShowGUI() {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            DelayedLabels demo = new DelayedLabels();
            demo.setOpaque(true);
            frame.add(demo, BorderLayout.PAGE_START);
            frame.pack();
            frame.setSize(200, 150);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    UIManager.put("swing.boldMetal", Boolean.FALSE);
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a JPanel which contains an arbitrary number of JLabels, laid out
I have a JFrame created with GUI builder of Netbeans, which contains a JPanel
I have created a JFrame and I want to add a panel appearing when
I have created jframe in which jpanels are added dynamically what i can't do
I created a chat panel and added to Jframe but the panel is not
i have created a JFrame in netbeans. But when i run the program, the
I've created an app but I have a problem with my JLabels not showing
I have created a jframe using net beans and add combo box to that.
I'm building simple Swing Java application, it contains JFrame and JPanel. My JPanel's constructor
I have created a JFrame with a JSplitPane in it, which contains a canvas

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.