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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:07:04+00:00 2026-05-20T11:07:04+00:00

I’m trying to get a SwingWorker to work. I’ve the following code at the

  • 0

I’m trying to get a SwingWorker to work.
I’ve the following code at the moment:

public class ImageWorker extends SwingWorker<Void, Void> implements KeyListener
{   
private JLabel imageLabel;
private ImageIcon basicImage; 
private ImageIcon whiteImage;  
public static void main(String[] args)
{
    new ImageWorker();      
}

public ImageWorker()
{
    final JFrame frame = new JFrame();
    imageLabel = new JLabel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.getContentPane().add(imageLabel);
    frame.setVisible(true);
    try
    {
        basicImage = new ImageIcon(ImageIO.read(new File("src\\img\\basis1.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));
        whiteImage = new ImageIcon(ImageIO.read(new File("src\\img\\wit.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));       
    }
    catch(IOException ex)
    {
        ex.getMessage();
    }
    this.execute();

}

@Override
protected Void doInBackground() 
{
    try
    {
        while (true)
        {
            displayImage(basicImage);
            Thread.sleep(1000L);
            if(isCancelled())
                return null;
        }
    }
    catch(InterruptedException e)
    {
        e.getMessage();
    }
    return null;
}

private void displayImage(final Icon image)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            imageLabel.setIcon(image);
        }
    });     
}

I was expecting the images to appear in the JLabel, but I only see the JFrame popping up. The files are loaded correctly Ive tested that in another setup.
Any pointers?

  • 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-05-20T11:07:04+00:00Added an answer on May 20, 2026 at 11:07 am

    Here is an example using a Timer rather than using the SwingWorker which really isn’t appropriate to your situation. Note that it’s not too different from your existing code.

    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.Timer;
    
    public class ImageWorker implements KeyListener
    {
        private JLabel imageLabel;
        private ImageIcon basicImage;
        private ImageIcon whiteImage;
        private boolean isBasic =  true;
        private int delay = 1000; //milliseconds
        private Timer timer;
    
        public static void main(String[] args)
        {
            new ImageWorker();
        }
    
        public ImageWorker()
        {
            final JFrame frame = new JFrame();
            imageLabel = new JLabel();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            frame.getContentPane().add(imageLabel);
            frame.setVisible(true);
    
            try
            {
                basicImage = new ImageIcon(ImageIO.read(new File("src\\img\\basis1.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));
                whiteImage = new ImageIcon(ImageIO.read(new File("src\\img\\wit.jpg")).getScaledInstance(1024, 768, Image.SCALE_SMOOTH));       
            }
            catch (IOException ex)
            {
                ex.getMessage();
                ex.printStackTrace();
            }
    
            frame.addKeyListener(this);
            ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    if(isBasic) {
                        //display basic image
                        imageLabel.setIcon(basicImage);
                    }
                    else {
                        //display white image
                        imageLabel.setIcon(whiteImage);
                    }
    
                    //toggle the flag
                    isBasic = !isBasic;
                }
            };
            //use a timer instead of SwingWorker
            timer = new Timer(delay, taskPerformer);
            timer.start();
    
        }
    
    
    
        @Override
        public void keyPressed(KeyEvent e)
        {
            //key pressed, we want to stop toggling so stop the timer
            timer.stop();
            //do whatever else you were doing to set the value for isCancelled();
        }
    
        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void keyTyped(KeyEvent e)
        {
            // TODO Auto-generated method stub
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.