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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:31:02+00:00 2026-05-15T04:31:02+00:00

I’m having problems in adding a picture into JFrame, something is missing probebly or

  • 0

I’m having problems in adding a picture into JFrame, something is missing probebly or written wrong.
here are the classes:

main class:

public class Tester

    {
        public static void main(String args[])
        {
            BorderLayoutFrame borderLayoutFrame = new BorderLayoutFrame();
            borderLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            borderLayoutFrame.setSize(600,600);
            borderLayoutFrame.setVisible(true);
        }
    }

public class BorderLayoutFrame extends JFrame implements ActionListener 
 {
     private JButton buttons[]; // array of buttons to hide portions
     private final String names[] = { "North", "South", "East", "West", "Center" };
     private BorderLayout layout; // borderlayout object
     private PicPanel picture = new PicPanel();

     // set up GUI and event handling

     public BorderLayoutFrame()
     {
         super( "Philosofic Problem" );
         layout = new BorderLayout( 5, 5 ); // 5 pixel gaps
         setLayout( layout ); // set frame layout
         buttons = new JButton[ names.length ]; // set size of array

         // create JButtons and register listeners for them

         for ( int count = 0; count < names.length; count++ ) 
         {
             buttons[ count ] = new JButton( names[ count ] );
             buttons[ count ].addActionListener( this );
         }
         add( buttons[ 0 ], BorderLayout.NORTH ); // add button to north
         add( buttons[ 1 ], BorderLayout.SOUTH ); // add button to south
         add( buttons[ 2 ], BorderLayout.EAST ); // add button to east
         add( buttons[ 3 ], BorderLayout.WEST ); // add button to west
         add( picture, BorderLayout.CENTER ); // add button to center
    }

    // handle button events

    public void actionPerformed( ActionEvent event )
    {

    } 

  }

I’v tried to add the image into the center of layout.

here is the image class:

public class PicPanel extends JPanel
{
    Image img;
    private int width = 0;
    private int height = 0;

    public PicPanel()
    {
        super();
        img = Toolkit.getDefaultToolkit().getImage("table.jpg");
    }
    public void paintComponent(Graphics g)
    {
         super.paintComponents(g);
         if ((width <= 0) || (height <= 0))
         {
             width = img.getWidth(this);
             height = img.getHeight(this);
         }
         g.drawImage(img,0,0,width,height,this);
    }
}

Please your help, what is the problem?
thanks

BTW: i’m using eclipse, which directory the image suppose to be in?

  • 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-15T04:31:03+00:00Added an answer on May 15, 2026 at 4:31 am

    There’s several issues with the code you’ve posted:

    • You should use getContentPane().add() instead of simply add() in your BorderLayoutFrame class.
    • You should really use SwingUtilities.invokeLater() to launch your JFrame from the tester class. Something like this:

     SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            System.setProperty("DEBUG_UI", "true");
    
            BorderLayoutFrame blf = new BorderLayoutFrame();
            blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            blf.setSize(600,600);
            blf.setVisible(true);
        }
    });
    
    • Don’t use Toolkit to load images! In the following code, if “Table.jpg” is in the same package as PicPanel, the image will correctly load.

    public PicPanel() {
        super();
        try {
            rUrl = getClass().getResource("Table.jpg");
            if (rUrl != null) {
                img = ImageIO.read(rUrl);
            }
        } catch (IOException ex) {
            Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    • In PicPanel.PaintComponent() you call super.paintComponents() is the ‘s’ a typeo?
    • In PicPanel.PaintComponent(), you don’t need all the width/height stuff, just do this:

      g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

    And avoid the call to super.paintComponent all together because you’re painting an image, why do you want the panel to paint at all?

    My final implementation of your stuff:

    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    System.setProperty("DEBUG_UI", "true");
    
                    BorderLayoutFrame blf = new BorderLayoutFrame();
                    blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    blf.setSize(600,600);
                    blf.setVisible(true);
                }
            });
        }
    
    }
    
    class BorderLayoutFrame extends JFrame implements ActionListener
    {
        private final BorderLayout layout;
        private final JButton[] buttons;
        private final String names[] = {"North", "South", "East", "West", "Center"};
    
        public BorderLayoutFrame() {
            super( "Philosofic Problem" );
            layout = new BorderLayout( 5, 5 );
            getContentPane().setLayout( layout );
            buttons = new JButton[ names.length ];
    
            for (int i=0; i<names.length; i++)
            {
                buttons[i] = new JButton(names[i]);
                buttons[i].addActionListener(this);
            }
    
            getContentPane().add(buttons[0], BorderLayout.NORTH);
            getContentPane().add(buttons[1], BorderLayout.SOUTH);
            getContentPane().add(buttons[2], BorderLayout.EAST);
            getContentPane().add(buttons[3], BorderLayout.WEST);
            getContentPane().add(new PicPanel(), BorderLayout.CENTER);
        }
    
        public void actionPerformed(ActionEvent e) {
            // ignore
        }
    
    }
    
    class PicPanel extends JPanel
    {
        private URL rUrl;
        private BufferedImage img;
    
    
    
        public PicPanel() {
            super();
            try {
                rUrl = getClass().getResource("UtilBtn.png");
                if (rUrl != null) {
                    img = ImageIO.read(rUrl);
                }
            } catch (IOException ex) {
                Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            //super.paintComponent(g);
    
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }
    
    }
    
    • 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 used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm having trouble keeping the paragraph square between the quote marks. In firefox 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.