可以为JFileChooser控件添加一个可访问组件,在windows系统中放置在JFileChooser的右边,高度与文件列表区的高度相同,宽度为可访问组件的首选宽度。可访问组件可以作为预览器,当浏览图片时可以显示缩小比例的图片,也可以预览文本文件的内容。为JFileChooser控件添加属性改变监听器,当文件选择发生改变时,触发属性改变事件,进行处理,可访问组件进行预览。JFileChooser.SELECTED_FILE_CHANGED_PROPERTY表示文件选择改变(单选)。
添加可访问组件方法
public void setAccessory(JComponent newAccessory)
例如可以放一个JLabel组件进行图片预览,代码如下:
JFileChooser jfc = new JFileChooser();
jfc.addChoosableFileFilter(new TextFileFilter());
jfc.addChoosableFileFilter(new PicFileFilter());
jfc.setMultiSelectionEnabled(false);
JLabel lab = new JLabel("");
lab.setHorizontalAlignment(SwingUtilities.CENTER);
lab.setVerticalAlignment(SwingUtilities.CENTER);
lab.setBorder(new LineBorder(Color.red));
lab.setPreferredSize(new Dimension(150,0));
jfc.setAccessory(lab);
jfc.addPropertyChangeListener(new PropertyChangeListener()
{
@Override
public void propertyChange(PropertyChangeEvent e)
{
// TODO Auto-generated method stub
if(e.getPropertyName()==JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
{
File file = (File) e.getNewValue();
if(file==null) return;
String name = file.getName();
if(file.isDirectory()) return;
if(name.endsWith("jpg")||name.endsWith("png"))
{
Image image = Toolkit.getDefaultToolkit().createImage(file.getAbsolutePath());
MediaTracker mt = new MediaTracker(jfc);
mt.addImage(image, 0);
try
{
mt.waitForID(0);
} catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
int w1 = image.getWidth(jfc);
int h1 = image.getHeight(jfc);
Dimension size = lab.getSize();
Insets inset = lab.getInsets();
int w = size.width - inset.left - inset.right;
int h = size.height - inset.top - inset.bottom;
int scale = w1/w > h1/h ? w1/w:h1/h;
Image image1 = image.getScaledInstance(w1/scale, h1/scale, Image.SCALE_SMOOTH);
lab.setIcon(new ImageIcon(image1));
}
}
}
});
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
运行如下:
txt文本文件和java文件预览器,代码如下:
JButton jbn_txt = new JButton("打开文本文件");
panel.add(jbn_txt);
jbn_txt.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JFileChooser jfc = new JFileChooser();
jfc.addChoosableFileFilter(new TextFileFilter());
jfc.addChoosableFileFilter(new PicFileFilter());
jfc.setMultiSelectionEnabled(false);
JTextArea jta = new JTextArea("sdf");
jta.setBorder(new LineBorder(Color.black));
//jta.setPreferredSize(new Dimension(150,0));
JScrollPane jsp = new JScrollPane(jta);
jsp.setPreferredSize(new Dimension(150,300));
jfc.setAccessory(jsp);
jfc.addPropertyChangeListener(new PropertyChangeListener()
{
@Override
public void propertyChange(PropertyChangeEvent e)
{
// TODO Auto-generated method stub
if(e.getPropertyName()==JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
{
File file = (File) e.getNewValue();
if(file==null) return;
String name = file.getName();
if(file.isDirectory()) return;
if(name.endsWith(".txt")||name.endsWith(".java"))
{
jta.setText("");
Scanner scan = null;
try
{
scan = new Scanner(file);
while(scan.hasNextLine())
{
String str = scan.nextLine();
jta.append(str+"\n");
}
} catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}finally
{
if(scan!=null)
scan.close();
}
}
}
}
});
利用JTextArea进行显示文件内容
运行如下:
由于水平有限,如果有错误,请大家多多指导,提高水平,共同学习。