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 8175437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:47:01+00:00 2026-06-06T22:47:01+00:00

i have a mainFrame that contains panel. i want in this panel a thread

  • 0

i have a mainFrame that contains panel. i want in this panel a thread that changes label image as long as the application is running…

when i created the panel that implements runnable, and then created an instance of this panel in the mainframe the application goes into infinite loop… my code as follows:

public mainFrame()
{
     BanerPanel baner = new BanerPanel();
     baner.run();
}

public class Banner_Panel extends JPanel implements Runnable {

    public Banner_Panel() {
        initComponents();
        imgPath = 2;
        imgLbl = new JLabel(new ImageIcon(getClass().getResource("/Photos/banner_4-01.png")));
        add(imgLbl);
        //run();
    }
    @Override
    public void run() {
        while(true)
        {
            try {
            while (true) {
                Thread.sleep(3000);
                switch(imgPath)
                {
                    case 1:
                        imgLbl.setIcon(new ImageIcon(getClass().getResource("/Photos/banner_4-01.png")));
                        imgPath = 2;
                        break;
                    case 2:
                        imgLbl.setIcon(new ImageIcon(getClass().getResource("/Photos/banner_1-01.png")));
                        imgPath = 3;
                        break;
                    case 3:
                        imgLbl.setIcon(new ImageIcon(getClass().getResource("/Photos/banner_2-01.png")));
                        imgPath = 4;
                        break;
                    case 4:
                        imgLbl.setIcon(new ImageIcon(getClass().getResource("/Photos/banner_3-01.png")));    
                        imgPath = 1;
                        break;
                }

            }
            } catch (InterruptedException iex) {}
        }
    }
  • 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-06T22:47:03+00:00Added an answer on June 6, 2026 at 10:47 pm
    • Don’t call JLabel#setIcon(...) in a background thread as this must be called on the Swing event thread or EDT. Instead, why not simply us a Swing Timer?
    • Also, there’s no need to continually read in the image from the disk. Instead, read the images in once and place the ImageIcons in an array or ArrayList<ImageIcon> and simply iterate through the icons in your Swing Timer.
    • Your code actually doesn’t use a background thread as you’re calling run() directly on your Runnable object which isn’t doing any threading at all. Please read the threading tutorial to see how to use Runnables and Threads (hint you call start() on the Thread).

    For example

    // LABEL_SWAP_TIMER_DELAY a constant int = 3000
    javax.swing.Timer myTimer = new javax.swing.Timer(LABEL_SWAP_TIMER_DELAY, 
          new ActionListener(){
      private int timerCounter = 0;
    
      actionPerformed(ActionEvent e) {
        // iconArray is an array of ImageIcons that holds your four icons.
        imgLbl.setIcon(iconArray[timerCounter]);
        timerCounter++;
        timerCounter %= iconArray.length;
      }
    });
    myTimer.start();
    

    For more, please check out the Swing Timer Tutorial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET application running on Windows 2003 that needs to communicate with
We have a Japanese client that has source code in COBOL on an mainframe.
Ok. I have a mainframe which contains two containers. One contains my wizard-alike buttons
I have 2 classes, mainFrame and panel. By clicking the button on mainFrame I
I have a CSS that makes fading out effect. #mainframe.normal { opacity: 1.0; }
I have seen application that are written in Java that can interface to CICS
i have a MVC application that works fine; but now i need to put
I have implemented an eclipse plugin that it launches a java application when the
In a doc-based ARC-enabled application I have a WebView that is opening an HTML
I have a WebView that I want to save as a WebArchive. I just

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.