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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:26:31+00:00 2026-06-09T07:26:31+00:00

so I’ve been working on my program and I am having trouble getting setting

  • 0

so I’ve been working on my program and I am having trouble getting setting up a JFrame with a background image of a network map and then setting a JPanel using a MigLayout on top of the JFrame which will allow me to place images in accordance to the network map. I have seen several different ways on how to set a background image and have tried using an example I found elsewhere on this site but it’s not quite working. Also, I researched more on this and found that having a JPanel ontop of a JFrame may prevent the JFrame background image from being seen. I was hoping that someone could take a look at my code and let me know if that is the case.

Here is my class to set the background:

public class bwMonBackground extends JFrame {
    Image img;
    public bwMonBackground() {
        try {
            img = ImageIO.read(new File("/Users/pwopperer/workspace/BWmon/src/customer_vlans.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
public void paintComponent(Graphics g)
{
    super.paintComponents(g);
    g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}

}

Here is my main:

public class bwMonBackgroundMain {

    public static void main( String[] args )
        {
            bwMonBackground frame = new bwMonBackground();
            frame.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
            migLayout testing = new migLayout();
            frame.add(testing.createLayout());
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();

        }
}

EDIT: When I run it like this, I only get a JPanel with the 6 JLabels I added into it

  • 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-09T07:26:32+00:00Added an answer on June 9, 2026 at 7:26 am

    It’s been a little nit picky, but you should always call super.paintComponent unless you have a REALLY, REALLY good reason not to.

    Also, you should provide Graphics.drawImage with a ImageObserver where possible. Basically this means if the image processing is taking place in another thread, the ImageObserver will not notified of changes and updated automatically 😉

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
    }
    

    When placing components onto your map, you need to make sure they a transparent (JComponent.setOpaque(false)). This will allow the background to show through.

    Obviously, you could choose some components to be opaque so they are more easily readable or provide AlphaComposites to provide translucent effects.

    Additional Ideas

    I wouldn’t override JFrame in this manner. Frames have content panes, which are responsible for the core content.

    Besides, from memory, JFrame doesn’t have a paintComponent method 😛

    I would instead override JPanel (or JComponent) and user there paintComponent method instead, then either add this panel to the frames content pane or set it as the content pane itself (frame.setContentPane(...) – just beware, you do this BEFORE adding any other components to the frame, as this will effectively remove them from the UI ;))

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.