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

  • Home
  • SEARCH
  • 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 1052497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:06:10+00:00 2026-05-16T17:06:10+00:00

I am trying to create a panel with changing pictures. This is my panel:

  • 0

I am trying to create a panel with changing pictures.
This is my panel:

public class AdvertisementPanel extends JPanel {
private BufferedImage image;

private ArrayList<String> pictures;
private int index = 0;

public AdvertisementPanel(String... pics) {
pictures = new ArrayList<String>(Arrays.asList(pics));
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
 new Runnable() {
  @Override
  public void run() {
   changeImage();
  }
}, 0, 5, TimeUnit.SECONDS);

}

public void paint(Graphics g) {
 g.drawImage(image, 0, 0, null);
}

private void changeImage() {
 String name = pictures.get(index);
try {
 File input = new File(name);
 image = ImageIO.read(input);
 index++;
 index %= pictures.size();
} catch (IOException ie) {
 Logger.getLogger().log(Level.SEVERE,
  "No adds found in given path: " + name);
}
}

I have a frame that holds the panel, but no pictures are shown.
Tried to repaint periodically from the frame – caused some funny, yet unwanted results…
Any ideas why? What am I doing wrong? How should I refresh the frame’s components?

  • 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-16T17:06:11+00:00Added an answer on May 16, 2026 at 5:06 pm

    you need to repaint each time you change the image.

    Oh, and it should be done by the swing event handling thread:

    Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
     new Runnable() {
      @Override
      public void run() {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            changeImage();
            repaint();
           }
        };
      }
    }, 0, 5, TimeUnit.SECONDS);
    

    UPDATE To correct a few other issues

    public class AdvertisementPanel extends JPanel {
        private BufferedImage image;
        private ArrayList<String> pictures;
        private int index = 0;
    
        public AdvertisementPanel(String... pics) {
            pictures = new ArrayList<String>(Arrays.asList(pics));
            Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
                new Runnable() {
                    @Override
                    public void run() {
                        changeImage();
                    }
                }, 0, 5, TimeUnit.SECONDS);
        }
    
        private void changeImage() {
            final BufferedImage img = nextImage();
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    image = img;
                    repaint();
                }
            });
        }
    
        public void paint(Graphics g) {
            if (image != null) {
                g.drawImage(image, 0, 0, null);
            }
        }
    
        private BufferedImage nextImage() {
            String name = pictures.get(index);
            try {
                index++;
                index %= pictures.size();
                File input = new File(name);
                return ImageIO.read(input);
            } catch (IOException ie) {
                Logger.getLogger("").log(Level.SEVERE,
                        "No adds found in given path: " + name);
                return null;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a mixing for Ext.grid.Panel components. This is how I have
I've been trying to create a custom control that works exactly like the Panel
I am trying to create a Swing panel whose elements have a different font
I am trying to create a panel which will have a set of buttons
I am trying to create a scrollable panel within a table cell. What I
I'm trying to create a navigation panel based on the Flex Accordion control. The
I have been trying to create a menu panel with jQuery that can be
I am trying to create a menu panel in jQuery, all is complete except
I'm trying to create a top panel menu that contains 5 buttons. I configured
I am trying to create a help panel for an application I am working

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.