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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:49:22+00:00 2026-05-14T08:49:22+00:00

This question has been asked a lot but everywhere the answers fall short. I

  • 0

This question has been asked a lot but everywhere the answers fall short. I can get a JFrame to display a background image just fine by extending JPanel and overriding paintComponent, like so:

class BackgroundPanel extends JPanel {
    private ImageIcon imageIcon;
    public BackgroundPanel() {
        this.imageIcon = Icons.getIcon("foo");
  }

    @Override
    protected void paintComponent(Graphics g) {
       super.paintComponent(g);
        g.drawImage(imageIcon.getImage(), 0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight(),this);
    }
}

But now, how do you add a component on top of that background?
When I go

JFrame w = new JFrame() ;
Container cp = w.getContentPane();
cp.setLayout(null);

BackgroundPanel bg = new BackgroundPanel();
cp.add(bg);

JPanel b = new JPanel();
b.setSize(new Dimension(30, 40));
b.setBackground(Color.red);
cp.add(b);
w.pack()
w.setVisible(true)

It shows the little red square (or any other component) and not the background, but when I remove cp.setLayout(null);, the background shows up but not my other component. I’m guessing this has something to do with the paintComponent not being called by the null LayoutManager, but I’m not at all familiar with how LayoutManagers work (this is a project for college and the assignment specifically says not to use a LayoutManager).

When i make the image the background has to display null (and so, transparant (??)) the red square shows up so it might be that the background is actually above my other components.

Does anyone anyone have any ideas?

Thanks

  • 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-14T08:49:22+00:00Added an answer on May 14, 2026 at 8:49 am

    When using null layout (and you almost never should) you have to supply a bounds for every component, otherwise it defaults to (0 x,0 y,0 width,0 height) and the component won’t display.

    BackgroundPanel bg = new BackgroundPanel();
    cp.add(bg);
    

    isn’t supplying a bounds. You’ll need to do something like:

    BackgroundPanel bg = new BackgroundPanel();
    bg.setBounds(100, 100, 100, 100);
    cp.add(bg);
    

    Which would make bg size 100 x 100 and place it at 100 x, 100 y on the frame.

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

Sidebar

Related Questions

This question has been asked before perhaps multiple times, but I can't get the
I know this question has been asked before, but I can't seem to get
This question has been asked a lot here, but i still can't fix my
Maybe this question has been asked a lot, but I still can't understand how
I know this question has already been asked a lot, but I don't get
Yes, I know this question has been asked a lot of times, but I
This question has been asked before but the answers aren't always clear or are
This question has been asked a lot of times in many forums. But I
I fully understand this question has been asked a lot , but I'm asking
This question has been asked a lot and there are quite a few answers,

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.