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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:39:53+00:00 2026-06-05T13:39:53+00:00

What is the best way to add a background image to a JPanel/JLabel when

  • 0

What is the best way to add a background image to a JPanel/JLabel when a JButton is called? I know how to get the JButton action and such. I just can’t figure out or find a way to get the background image to change when that button is pressed.

  • 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-05T13:39:54+00:00Added an answer on June 5, 2026 at 1:39 pm

    Here is an example:

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;
    import java.util.concurrent.ExecutionException;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.SwingWorker;
    
    public class ModifiableBackgroundFrame extends JFrame implements ActionListener
    {
        private static final long serialVersionUID = 1L;
        private ImageIcon image;
        private JPanel pan;
        private JButton btn;
        private int count = 0;
        private static final String[] images = 
            {"http://www.dvd-ppt-slideshow.com/images/ppt-background/background-3.jpg",
            "http://www.psdgraphics.com/wp-content/uploads/2009/02/abstract-background.jpg",
            "http://hdwallpaperpics.com/wallpaper/picture/image/background.jpg",
            "http://www.highresolutionpics.info/wp-content/uploads/images/beautiful-on-green-backgrounds-for-powerpoint.jpg"};
    
        public ModifiableBackgroundFrame()
        {
            super("The title");
    
            image = new ImageIcon();
    
            btn = new JButton("Change background");
            btn.setFocusPainted(false);
            btn.addActionListener(this);
            pan = new JPanel()
            {
                private static final long serialVersionUID = 1L;
    
                @Override
                public void paintComponent(Graphics g)
                {
                    g.drawImage(image.getImage(), 0, 0, null);
                }
            };
            pan.setPreferredSize(new Dimension(400, 400));
    
            Container contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout());
            contentPane.add(pan, BorderLayout.CENTER);
            contentPane.add(btn, BorderLayout.SOUTH);
    
            pack();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new ModifiableBackgroundFrame();
                }
            });
        }
    
        @Override
        public void actionPerformed(ActionEvent e)
        {
            btn.setEnabled(false);
            btn.setText("Loading...");
            new SwingWorker<Image, Void>()
            {
                @Override
                protected Image doInBackground() throws Exception
                {
                    return ImageIO.read(new URL(images[count++ % 4]));
                }
    
                @Override
                protected void done()
                {
                    try
                    {
                        image.setImage(get());
                        pan.repaint();
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    catch(ExecutionException e)
                    {
                        e.printStackTrace();
                    }
                    btn.setText("Change background");
                    btn.setEnabled(true);
                }
            }.execute();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know, which is the best way to blur the background of
What is the best way to add bullet-like images before text? I can set
What is the best way to add text to the beginning of a file
What is the best way to add a sense of order in Doctrine Nested
What's the best way to add foreign keys to my existing tables in Rails
I What is the best way to add multiple layers with the help of
Given a large table (10-100 million rows) what's the best way to add some
What is the best way for me to add my own properties to an
What would be the best way to add projections to an nhibernate query that
I am trying to add a background image to UITableViewCell using the following code:

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.