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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:38:00+00:00 2026-06-18T03:38:00+00:00

I tried loading images, I found a code and it works well, the code

  • 0

I tried loading images, I found a code and it works well, the code for loading an Image is in the constructor, I tried to move it to a button action, and also to a filechooser. But never load an image. there are no errors in the code, and I tested the clauses using print statements, they show that the clauses are passed.

this is an SSCCE:

The constructor code is separated into two parts Upper and Lower. To demonstrate the problem alternate between these codes by commenting and uncommentting each.

the Lower part will work; it loads the image from the constructor.
the upper part set the JPanels and the JButtons required to load the image from Action code.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class ImgLoadTest extends JPanel implements ActionListener
{

    File file;
    BufferedImage img;
    JButton fileChooserButton;
    JButton showImgButton;
    JPanel imgePanel;
    JPanel widgetPanel;
    JFileChooser fileChooser;

    @Override
    public void paintComponent(Graphics g)
    {
        g.drawImage(img, 0, 60, null);
    }

    public ImgLoadTest()
    {
           //Upper:

//        fileChooser = new JFileChooser();
//
//        fileChooserButton = new JButton("Browse");
//        fileChooserButton.addActionListener(this);
//        showImgButton = new JButton("Show Image");
//        showImgButton.addActionListener(this);
//
//        imgePanel = new JPanel();
//        widgetPanel = new JPanel();
//        this.setLayout(new BorderLayout());
//        widgetPanel.add(fileChooserButton);
//        widgetPanel.add(showImgButton);
//        this.add(imgePanel, BorderLayout.CENTER);
//        this.add(widgetPanel, BorderLayout.EAST);


        /*
         *  try alternating (comment/uncomment) between 
         *  the upper code (in this constructor)
         *  and the lower code below;
         */

          //Lower:

        try
        {
            img = ImageIO.read(new File("D:\\De.JPG"));
        } catch (IOException e)
        {
        }

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == fileChooserButton)
        {
            int reternedValue = fileChooser.showOpenDialog(ImgLoadTest.this);

            if (reternedValue == JFileChooser.APPROVE_OPTION)
            {
                file = fileChooser.getSelectedFile();
                try
                {
                    img = ImageIO.read(file);
                    System.out.println("File chooser: Image accepted");
                    this.repaint();

                } catch (IOException err)
                {
                    System.out.println("error " + err);
                }
            } else
            {
                System.out.println("No Image");
            }
        }
        if (source == showImgButton)
        {
            try
            {
                System.out.println("button: Image accepted");
                img = ImageIO.read(new File("D:\\De.JPG"));
                repaint();
            } catch (IOException err)
            {
                System.out.println("No Image");
                System.out.println("error " + err);
            }
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Image load test");
        frame.setSize(new Dimension(400, 600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImgLoadTest imgLoadTest = new ImgLoadTest();
        frame.add(imgLoadTest);
        frame.setVisible(true);
    }
}
  • 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-18T03:38:01+00:00Added an answer on June 18, 2026 at 3:38 am

    Just assigning the BufferedImage img will not make the image appear. You could add a JLabel which contains the image:

    JLabel picLabel = new JLabel(new ImageIcon(img));
    imgePanel.add(picLabel);
    imgePanel.revalidate();
    imgePanel.repaint();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried running the code without the collision, and it works fine. But
I tried to download an image using wget but got an error like the
I have a requirement of loading images but when i am loading images it
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
Tried searching the site, but cannot find an answer to my problem: Lets say
Tried a bunch of things but I can't get it to work consistently amid
tried to find the answer by googling and in MSDN but with no luck.
Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
I'm trying to show a small loading image during a slow operation with jQuery
I'm testing both LazyList (LL) and Universal Image Loader (UIL) that I found here

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.