`
djsl6071
  • 浏览: 578735 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

实现JFileChooser中的文件类型过滤器和图片预览缩略图

阅读更多

第二个Issue的进度很怪,谈不上快还是慢,事实是从网上找了好久找到了一些相关的例子,以此,可以初步作出一个符合要求的"Add image preview window to JFileChooser. add an Image Filter, only image file can be choosed.  "

例子来自网上,图片预览控件借助JFileChooser.setAccessory(JComponent newAccessory)添加,JFileChooser类封装得死死的,什么也挖不出来,先后找到了FileFilter、FileSystemView、FileView,借此实现了针对任意扩展名的Filter,Filter解决得差不多,反过来image preview又需要完善了,希望呈现一种windows环境中文件浏览器可以提供的所谓Thumbnails(缩略图),回过头去狂查JFileChooser,不知该从哪里入手,先后在FileView(用图片本身代替文件固有图标)和FileSystemView(借用系统本身标准文件系统的图标)中找到一些有用的东西,二者互助实现了稍微好一点的缩略图策略,当然我个人还不是很满意,为什么就不能想windows里面那样给我痛快地显示出来呢?问题估计还是sun的JFileChooser太旧了,也不更新下,你说就算不更新,也给我们一些开放度大点的东西吧,啧啧,非逼得我挑剔。期间,试用了UIManager的Look and Feel,找到windows下面的,看来下还凑合,只是左边的快捷图标怎么没有scrollpane呢?郁闷死我。

任务仍在进一步完善中,我也逐渐培养自己写这种业余水平的总结心得的习惯,但愿以后既有很规范的文档总结,有能有我自己独特的心路历程吧。以下附代码,比较长,中间尽量也放些讲解,技术blog可能这点好很多吧,其他的都还限制长度。

ps:中间一度查阅提供给我的英文网页,看的吐血了n次。以后还会碰到很多,初步需要jdk中的demo和api 源代码,其次借助tutorial、doc和javaworld等其他一些文档资料,进一步了解java,还下了一个java实用程序设计100例原代码和素材,不知有用没。

1. FileChooserDemo,主演示类,在第三个按钮下是JFileChooser,里面添加预览和Filter

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class FileChooserDemo extends JFrame ...{
    
private JButton btn1, btn2, btn3, btn4;

    
private JTextArea area;

    
private ObjectOutputStream output;

    
private JScrollPane scroll;

    
private JPanel panel;

    
public FileChooserDemo() ...{
        
super("FileChooser Demo");

        btn1 
= new JButton("Open Directory");
        btn1.addActionListener(
new ActionListener() ...{
            
public void actionPerformed(ActionEvent e) ...{
                JFileChooser choose 
= new JFileChooser();
                choose.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

                
int result = choose.showOpenDialog(null);

                
if (result == JFileChooser.APPROVE_OPTION)// if approve (yes,
                
// ok) is chosen
                ...{
                    File file 
= choose.getSelectedFile();

                    
if (!file.exists())
                        area.append(
"File does not exist~!");

                    
else if (file.isDirectory()) ...{
                        area.append(
"Directory " + file.getName()
                                
+ " containt following files:  ");
                        
for (int i = 0; i < file.list().length; i++)
                            area.append(file.list()[i] 
+ " ");
                    }
 else if (file.isFile())
                        area
                                .append(
"It is a file, please use the second button to hava another test ");
                }
 else
                    
return;
            }

        }
);

        btn2 
= new JButton("Open a file");
        btn2.addActionListener(
new ActionListener() ...{
            
public void actionPerformed(ActionEvent e) ...{
                JFileChooser choose 
= new JFileChooser();
                choose.setFileSelectionMode(JFileChooser.FILES_ONLY);

                
int result = choose.showOpenDialog(null);

                
if (result == JFileChooser.APPROVE_OPTION)// if approve (yes,
                
// ok) is chosen
                ...{
                    File file 
= choose.getSelectedFile();

                    
if (!file.exists())
                        area.append(
"File does not exist~!");

                    
else ...{
                        
try ...{
                            output 
= new ObjectOutputStream(
                                    
new FileOutputStream(file));
                            
// .....
                            
// tobeContinued
                        }
 catch (IOException ioexception) ...{

                            area.append(ioexception.toString());
                        }

                    }

                }
 else
                    
return;
            }

        }
);

        btn3 
= new JButton("Images File Filter");
        btn3.addActionListener(
new ActionListener() ...{
            
public void actionPerformed(ActionEvent e) ...{
                JFileChooser choose 
= new JFileChooser();

                
// add ImagePreviewPanel Accessory
                ImagePreviewPanel preview = new ImagePreviewPanel();
                choose.setAccessory(preview);
                choose.addPropertyChangeListener(preview);
                choose.setFileSelectionMode(JFileChooser.FILES_ONLY);

                
/**//*
                 * //add FindAccessory FindAccessory search = new
                 * FindAccessory(); choose.setAccessory(search);
                 * choose.addPropertyChangeListener(search);
                 * choose.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                 
*/


                
// add Filters
                String htmlExts[] = ..."html""htm" };
                String imageExts[] 
= ..."jpg""jpeg""png""gif","bmp" };

                
// construct it giving it an array of file
                
// extensions and a description string
                GenericFileFilter filter = new GenericFileFilter(htmlExts,
                        
"HTML Files");
                choose.addChoosableFileFilter(filter);

                filter 
= new GenericFileFilter(imageExts,
                        
"Images Files(*.jpg;*.jpeg;*.png;*.gif;*.bmp)");
                choose.addChoosableFileFilter(filter);

                choose.setFileView(
new GenericFileView(imageExts,
                        GenericFileView.IMAGE_FILEVIEW));

                
/**//*
                 * addChoosableFileFilter(Filter filter) and
                 * setFileFilter(Filter filter) are two familiar methods,they
                 * both add the filter to the JFileChooser, the difference is
                 * the setFileFilter set itself to the default choose.
                 
*/


                
int result = choose.showOpenDialog(null);

                
if (result == JFileChooser.APPROVE_OPTION)// if approve (yes,
                
// ok) is chosen
                ...{
                    File file 
= choose.getSelectedFile();

                    
if (!file.exists())
                        area.append(
"File does not exist~!");

                    
else
                        area.append(
"You chose " + file.getName() + " ");
                }
 else
                    
return;
            }

    &nb
分享到:
评论
1 楼 ydsakyclguozi 2009-03-15  
老兄,你这跑不起来啊

相关推荐

Global site tag (gtag.js) - Google Analytics