File Chooser with preview panel?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Alexey_Stupishin
Posted On:   Monday, September 2, 2002 05:49 AM

I need a swing component, like JFileChooser, but with additional panel, where I can place some info about currently selected file ("preview"). Does anybody know how I can customize JFileChooser, or maybe such component (or just sketch of the code) already available somewhere?
Thanks!

Re: File Chooser with preview panel?

Posted By:   Mike_Friedrich  
Posted On:   Tuesday, September 3, 2002 12:53 AM

Here is the example/suggestion from SUN's tutorials.
As you can see the preview is done at the event JFileChooser.SELECTED_FILE_CHANGED_PROPERTY. If You need to preview something else modify the code as needed.

JFileChooser fc = new JFileChooser(new java.io.File("images"));
fc.setAccessory(new de.swbee.swing.ImagePreview(fc));

...

public class ImagePreview extends JComponent implements PropertyChangeListener
{
ImageIcon thumbnail = null;
File file = null;

public ImagePreview(JFileChooser fc) {
setPreferredSize(new Dimension(100, 50));
fc.addPropertyChangeListener(this);
}

public void loadImage() {
if (file == null) {
return;
}

ImageIcon tmpIcon = new ImageIcon(file.getPath());
if (tmpIcon.getIconWidth() > 90) {
thumbnail = new ImageIcon(tmpIcon.getImage().
getScaledInstance(90, -1,
Image.SCALE_DEFAULT));
} else {
thumbnail = tmpIcon;
}
}

public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
file = (File) e.getNewValue();
if (isShowing()) {
loadImage();
repaint();
}
}
}

public void paintComponent(Graphics g) {
if (thumbnail == null) {
loadImage();
}
if (thumbnail != null) {
int x = getWidth()/2 - thumbnail.getIconWidth()/2;
int y = getHeight()/2 - thumbnail.getIconHeight()/2;

if (y < 0) {
y = 0;
}

if (x < 5) {
x = 5;
}
thumbnail.paintIcon(this, g, x, y);
}
}
}


Regards, Mike

About | Sitemap | Contact