mauricioadl 16 de abr. de 2012
pelo que eu saiba o JFileChooser não faz isso.
Allexb10 16 de abr. de 2012
Sim, gostaria de saber se há alguma forma de fazer isto.
Ou com JFileChooser ou de outra forma.
mauricioadl 16 de abr. de 2012
então, fui fazer uma pesquisa e vi que tem um tal de FileView que vc cria os icones. talvez de pra fazer oq vc quer.
exemplo:
http://www.java2s.com/Code/JavaAPI/javax.swing/JFileChoosersetFileViewFileViewfileView.htm
no pior dos casos, fazer um filechooser de imagens na “unha” nao deve ser tao dificil.
[]'s
mauricioadl 16 de abr. de 2012
import java.awt.Color ;
import java.awt.Dimension ;
import java.awt.Graphics ;
import java.awt.Image ;
import java.beans.PropertyChangeEvent ;
import java.beans.PropertyChangeListener ;
import java.io.File ;
import javax.swing.ImageIcon ;
import javax.swing.JFileChooser ;
import javax.swing.JPanel ;
public class ImagePreviewPanel extends JPanel implements PropertyChangeListener {
private static final long serialVersionUID = 2818093906032707079L ;
private int width , height ;
private ImageIcon icon ;
private Image image ;
private static final int ACCSIZE = 155 ;
private Color bg ;
public ImagePreviewPanel () {
setPreferredSize ( new Dimension ( ACCSIZE , - 1 ));
bg = getBackground ();
}
public void propertyChange ( PropertyChangeEvent e ) {
String propertyName = e . getPropertyName ();
if ( propertyName . equals ( JFileChooser . SELECTED_FILE_CHANGED_PROPERTY )) {
File selection = ( File ) e . getNewValue ();
String name ;
if ( selection == null )
return ;
else
name = selection . getAbsolutePath ();
if (( name != null ) && name . toLowerCase (). endsWith ( & quot ;. jpg & quot ;)
|| name . toLowerCase (). endsWith ( & quot ;. jpeg & quot ;)
|| name . toLowerCase (). endsWith ( & quot ;. gif & quot ;)
|| name . toLowerCase (). endsWith ( & quot ;. png & quot ;)) {
icon = new ImageIcon ( name );
image = icon . getImage ();
scaleImage ();
repaint ();
}
}
}
private void scaleImage () {
width = image . getWidth ( this );
height = image . getHeight ( this );
double ratio = 1.0 ;
if ( width & gt ; = height ) {
ratio = ( double ) ( ACCSIZE - 5 ) / width ;
width = ACCSIZE - 5 ;
height = ( int ) ( height * ratio );
} else {
if ( getHeight () & gt ; 150 ) {
ratio = ( double ) ( ACCSIZE - 5 ) / height ;
height = ACCSIZE - 5 ;
width = ( int ) ( width * ratio );
} else {
ratio = ( double ) getHeight () / height ;
height = getHeight ();
width = ( int ) ( width * ratio );
}
}
image = image . getScaledInstance ( width , height , Image . SCALE_DEFAULT );
}
public void paintComponent ( Graphics g ) {
g . setColor ( bg );
g . fillRect ( 0 , 0 , ACCSIZE , getHeight ());
g . drawImage ( image , getWidth () / 2 - width / 2 + 5 , getHeight () / 2
- height / 2 , this );
}
}
mauricioadl 16 de abr. de 2012
JFileChooser chooser = new JFileChooser();
ImagePreviewPanel preview = new ImagePreviewPanel();
chooser.setAccessory(preview);
chooser.addPropertyChangeListener(preview);
chooser.showOpenDialog(null);
Allexb10 16 de abr. de 2012
Valeu Mauricio, deu certinho.
Abraço!