Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems in Using JLabel to Display a Chosen Image File
    primarykey
    data
    text
    <p>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".</p> <p>Initially, I was managed to display the chosen image in <code>JLabel</code>, but later I created a separate <code>ActionListener</code>, I think it started to go wrong since then. Whatever image I choose, the <code>JLabel</code> won't display it.</p> <p>I debugged it, and sure that the file chooser does pass the image to <code>ImageIcon</code>, <code>JLabel</code> does get the value from <code>ImageIcon</code>, but it doesn't display the image even after <code>revalidate()</code> and <code>repaint()</code>.</p> <p>Here I attached my code for your kind reference!</p> <p>(I trimmed the code for a clean look, so there might be some brackets left not useful)</p> <pre class="lang-java prettyprint-override"><code>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(); } } } } </code></pre> <p>I also referenced other similar questions:</p> <p><a href="https://stackoverflow.com/questions/10737029/image-loading-using-a-jfilechooser-into-a-jframe">image loading using a JFileChooser into a JFrame</a></p> <p><a href="https://stackoverflow.com/questions/8883937/image-wont-display-in-jlabel">Image won&#39;t display in JLabel</a></p> <p><a href="https://stackoverflow.com/questions/10051638/updating-an-image-contained-in-a-jlabel-problems">Updating an image contained in a JLabel - problems</a></p> <p>External Site: <a href="http://www.dreamincode.net/forums/topic/253216-jfilechooser-opening-image-to-jlabel/" rel="nofollow noreferrer">JFIleChooser opening image to JLabel</a></p> <p>As well as Java Tutorial Docs <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html" rel="nofollow noreferrer">How to Use Buttons, Check Boxes, and Radio Buttons</a></p> <p>But I still can't figure it out why the JLabel not display the chosen image.</p> <p>Thanks for your kind help mates!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload