File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Displaying witrh respect to path.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Displaying witrh respect to path.." Watch "Displaying witrh respect to path.." New topic

Displaying witrh respect to path..

Manoj Paul
Ranch Hand

Joined: Jan 18, 2007
Posts: 112
Hello dudes

In my application for image display, those images are displayed which resides in the same folder as my program(code) is.... But now i want to display those images of folder respective to which an user click on a particular folder of local drives on a JTree... I am sending codes which i have written... Please take a view... and please make any necessary changes and reply with the changed codes.

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ThumbnailTest
Color bgColor;

public ThumbnailTest(SnapsGallery obj)

bgColor = UIManager.getColor("Panel.background");
BufferedImage[] images = loadImages();
BufferedImage[] tnails = createThumbnails(images);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
gbc.weightx = 1.0;
for(int j = 0; j < images.length; j++)
gbc.gridwidth = gbc.RELATIVE;
panel.add(new JLabel(new ImageIcon(images[j])), gbc);
// obj.jPanel2.add(new JLabel(new ImageIcon(images[j])), gbc);
gbc.gridwidth = gbc.REMAINDER;
panel.add(new JLabel(new ImageIcon(tnails[j])), gbc);
obj.jPanel2.add(new JLabel(new ImageIcon(tnails[j])), gbc);
JFrame f = new JFrame();
f.add(new JScrollPane(panel)); // j2se 1.5
//f.getContentPane().add(new JScrollPane(panel)); // j2se 1.4
// f.setVisible(true);

private BufferedImage[] createThumbnails(BufferedImage[] origs)
final int WIDTH = 100;
final int HEIGHT = 175;
BufferedImage[] images = new BufferedImage[origs.length];
AffineTransform at;
for(int j = 0; j < origs.length; j++)
images[j] = new BufferedImage(WIDTH, HEIGHT,
Graphics2D g2 = images[j].createGraphics();
g2.fillRect(0, 0, WIDTH, HEIGHT);
// scale to fit
double xScale = (double)WIDTH / origs[j].getWidth();
double yScale = (double)HEIGHT / origs[j].getHeight();
double scale = Math.min(xScale, yScale);
// center thumbnail image
double x = (WIDTH - origs[j].getWidth() * scale)/2;
double y = (HEIGHT - origs[j].getHeight() * scale)/2;
at = AffineTransform.getTranslateInstance(x, y);
at.scale(scale, scale);
g2.drawRenderedImage(origs[j], at);
return images;

private BufferedImage[] loadImages()
String prefix = "images\\";
String ext = ".jpg";
String[] fileNames = {
"2", "3", "5", "2"
BufferedImage[] images = new BufferedImage[fileNames.length];
for(int j = 0; j < fileNames.length; j++)
URL url = getClass().getResource(prefix + fileNames[j] + ext);
images[j] =;
catch(MalformedURLException mue)
System.out.println("url: " + mue.getMessage());
catch(IOException ioe)
System.out.println("read: " + ioe.getMessage());
return images;
Here in the codes the file names are clearly given in the code and so those images are displayed... But now i want to display any image of the local drives according to user click on the JTree..
I am sure i need to change particularly this method...
private BufferedImage[] loadImages()

But i dont know what to change in this method... Please help

Waiting for your reply...
Thank you
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Please don't post the same question in multiple forums. It wastes people's time when they answer questions that have already been answered elsewhere.
[ April 09, 2007: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Closing this thread. Direct further discussion to the other thread.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Displaying witrh respect to path..
jQuery in Action, 3rd edition