Re: Display jpg in JPanel problem

On 6/23/2010 3:18 AM, jimmy wrote:
Hi All,

I am developing a GUI to display jpg images in a JPanel. I have
created two ways for the images to be selected: (i) a JFileChooser
button to select a single jpg, and (ii) a JFileChooser to select a
directory containing jpg files. Once the images have been selected,
they should be displayed on the JPanel.

In the case of opening the single file, this works as I hoped. However
when the open directory scenario is used, nothing is displayed.
Through the use of println statements I have been able to establish
the programme is executing the same Classes and Methods as in the
single image display. In addition to displaying an image, I have a
JLabel in which I display the name of the current image. Again for the
single image file selection, this works. For the open directory
scenario, only the name of the final image in the directory is
displayed (without the image).

Thinking that maybe there was not sufficient time for the image to be
displayed, I added a Thread.sleep(4000); line, but this did not help.

Included below are the two actionPerformed methods from each button,
plus the DrawImage and MyFilter Classes. I would appreciate if anyone
could indicate possible causes for the failure of the open directory
display method.

Many thanks,

Instead of drawing the image yourself, you should consider using JLabel's "setIcon" method and an ImageIcon instance. It makes adding images much easier, and is very flexible.


Daniel Pitts' Tech Blog: <>