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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:05:24+00:00 2026-06-07T03:05:24+00:00

I encountered a problem while I am trying to display an image after I

  • 0

I encountered a problem while I am trying to display an image after I clicked a button and chose image file within the “Choose File Dialog”.

Initially, I was managed to display the chosen image in JLabel, but later I created a separate ActionListener, I think it started to go wrong since then. Whatever image I choose, the JLabel won’t display it.

I debugged it, and sure that the file chooser does pass the image to ImageIcon, JLabel does get the value from ImageIcon, but it doesn’t display the image even after revalidate() and repaint().

Here I attached my code for your kind reference!

(I trimmed the code for a clean look, so there might be some brackets left not useful)

package com.xxx.LoyalCardManager;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

public class LoyalCardManagerMain implements ActionListener{

private JFrame frame;
private DatabaseHandler db = new DatabaseHandler();


private JLabel labelPic;

private JButton buttonPic;

private File picFile = new File("");
private BufferedImage image;


/**
 * Launch the application.
 * @throws SQLException 
 * @throws ClassNotFoundException 
 */
public static void main(String[] args) throws SQLException, ClassNotFoundException {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                LoyalCardManagerMain window = new LoyalCardManagerMain();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


}



}

/**
 * Create the application.
 */
public LoyalCardManagerMain() {

    // Database initialisation
    initDatabase();

    // Draw GUI
    frame = new JFrame();
    frame.setBounds(100, 100, 619, 487);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    buttonPic = new JButton("Click to Choose Pic");
    buttonPic.setBounds(415, 252, 166, 29);
    frame.getContentPane().add(buttonPic);
    buttonPic.setEnabled(false);
    buttonPic.setActionCommand("ChoosePic");
    buttonPic.addActionListener(this);

    labelPic = new JLabel();
    labelPic.setBounds(415, 30, 167, 210);
    frame.getContentPane().add(labelPic);




}



public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();

     if (command.equals("ChoosePic")) {
        //TODO Label now cannot display images.
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.setFileFilter(new FileFilter() {
            public boolean accept (File f) {
                String extension = Utils.getExtension(f);
                if(extension != null) {
                    if (extension.equals(Utils.gif) ||
                        extension.equals(Utils.jpeg) ||
                        extension.equals(Utils.jpg) ||
                        extension.equals(Utils.png) ||
                        extension.equals(Utils.tif) ||
                        extension.equals(Utils.tiff)) {
                        return true;
                    }else{
                        return false;
                    }
                }
                return false;
            }

            public String getDescription() {
                return "Image File (*.gif, *.jpeg, *.jpg, *.png, *.tif, *.tiff)";
            }

        });

        int retVal = chooser.showOpenDialog(frame);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            picFile = chooser.getSelectedFile();
            try {
                image = ImageIO.read(picFile);
            } catch (IOException e) {

                e.printStackTrace();
            }

            // Calculate the pic's ratio and do re-scale

            double ratio = (double) labelPic.getWidth() / (double) labelPic.getHeight();
            // Do image scale, scaledW is the new Width, and LabelPic.getHeight is the new Height.
            int scaledW = (int) (image.getHeight() * ratio);
            image = new BufferedImage(scaledW, labelPic.getHeight(), BufferedImage.SCALE_FAST);
            ImageIcon icon = new ImageIcon(image);

            labelPic.setVisible(true);
            labelPic.setIcon(icon);
            labelPic.revalidate();
            labelPic.repaint();

        }


    }
}
}

I also referenced other similar questions:

image loading using a JFileChooser into a JFrame

Image won't display in JLabel

Updating an image contained in a JLabel – problems

External Site: JFIleChooser opening image to JLabel

As well as Java Tutorial Docs
How to Use Buttons, Check Boxes, and Radio Buttons

But I still can’t figure it out why the JLabel not display the chosen image.

Thanks for your kind help mates!

  • 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-07T03:05:25+00:00Added an answer on June 7, 2026 at 3:05 am

    Ok, I finally figured out what’s wrong with the code:

    If I intend to use BufferedImage to resize (sorry, in my question I mis-understanding the method scale with resize), I need to use drawImage method to “redraw” the image. Otherwise the image will not be shown.

    I made modification here:

    double ratio = (double) labelPic.getWidth() / (double) labelPic.getHeight();
            // Do image scale, scaledW is the new Width, and LabelPic.getHeight is the new Height.
            int scaledW = (int) (image.getHeight() * ratio);
            image = new BufferedImage(scaledW, labelPic.getHeight(), BufferedImage.SCALE_FAST);// Edit here
            ImageIcon icon = new ImageIcon(image);
    
            labelPic.setVisible(true);
            labelPic.setIcon(icon);
            labelPic.revalidate();
            labelPic.repaint();
    

    From the “Edit Here” mark, I use the following code:

    BufferedImage imageTemp = new BufferedImage(resizedW, resizedH, BufferedImage.TYPE_INT_RGB);
                imageTemp.getGraphics().drawImage(image,0,0, scaledW, scaledH, null);
                image = imageTemp;
    

    And there’s difference between first pass the value to imageTemp then pass to image and directly pass the value to image. If I pass the new BufferedImage directly to image, it will display a pure black colour instead of the image you choose.

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

Sidebar

Related Questions

I've encountered this problem (while trying to add SQL Server Database (.mdf) file to
I've encountered an curious problem while trying to use some objects through JSNI in
I've encountered a problem while trying to use the answer from a NDSolve in
I'm working with MVC recently and I've encountered a strange problem while trying to
I'm new to working with XML, and I've encountered a weird problem while trying
I'm trying to style input type:file. Problem I've encountered is that in Firefox I
While making AutoHotkey-script I encountered the following problem. I need navigate listbox (one position
While playing with NLP I've been encountered with little problem: switch(var1) { case Variant1_1:
while designing my user control, i encountered the following problem: i would like to
While tracing my modules using dbg, I encountered with the problem how to collect

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.