1 package snow.utils.gui; 2 3 import SnowMailClient.utils.MailMessageUtils; 4 import javax.swing.*; 5 import javax.swing.border.*; 6 import javax.imageio.*; 7 import java.awt.image.*; 8 import java.awt.*; 9 import java.util.*; 10 import java.beans.*; 11 12 14 public class FileChooserImageAccessoryPanel extends JPanel 15 { 16 final private JLabel imageLabel = new JLabel(); 17 final private JTextArea imageNameLabel = new JTextArea("",2,30); 18 20 public FileChooserImageAccessoryPanel(final JFileChooser fs) 21 { 22 super(new BorderLayout()); 23 24 add(imageLabel, BorderLayout.CENTER); 25 26 add(imageNameLabel, BorderLayout.SOUTH); 28 imageNameLabel.setBackground(imageLabel.getBackground()); 29 imageNameLabel.setFont(imageLabel.getFont()); 30 imageNameLabel.setEditable(false); 31 32 this.setBorder(new EmptyBorder(4,4,4,4)); 33 this.setPreferredSize(new Dimension(200,300)); 34 imageLabel.setHorizontalAlignment(JLabel.CENTER); 35 imageLabel.setVerticalAlignment(JLabel.CENTER); 36 37 fs.addPropertyChangeListener(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY, 38 new PropertyChangeListener() 39 { 40 public void propertyChange(PropertyChangeEvent evt) 41 { 42 try 44 { 45 imageNameLabel.setText("" + fs.getSelectedFile().getName() 46 + "\nSize: " + MailMessageUtils.formatSize(fs.getSelectedFile().length())); 47 48 BufferedImage bim = ImageIO.read( fs.getSelectedFile() ); 49 50 imageNameLabel.append("\ndim: "+bim.getWidth() + " x "+bim.getHeight()); 51 52 double redfact = (double) imageLabel.getWidth() / bim.getWidth(); 53 double f2 = (double) imageLabel.getHeight() / bim.getHeight(); 54 if(f2 < redfact) redfact = f2; 55 if(redfact<1) 56 { 57 imageLabel.setIcon(new ImageIcon(bim.getScaledInstance( 58 (int)(bim.getWidth() *redfact), 59 (int)(bim.getHeight()*redfact), bim.SCALE_SMOOTH))); 60 } 61 else 62 { 63 imageLabel.setIcon( new ImageIcon(bim) ); 64 } 65 } 66 catch(Exception e) 67 { 68 imageLabel.setIcon(null); 69 } 71 } 72 } 73 ); 74 } 76 77 79 public static void main(String [] a) 80 { 81 JFileChooser fs = new JFileChooser( "c:/proj" ); 82 fs.setAccessory(new FileChooserImageAccessoryPanel(fs)) ; 83 fs.showOpenDialog(null); 84 } 85 86 87 } | Popular Tags |