JFileChooser chooser = new JFileChooser(); // Note: source for ExampleFileFilter can be found in FileChooserDemo, // under the demo/jfc directory in the Java 2 SDK, Standard Edition. ExampleFileFilter filter = new ExampleFileFilter(); filter.addExtension("jpg"); filter.addExtension("gif"); filter.setDescription("JPG & GIF Images"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); }
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the Java 2 SDK, Standard Edition.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
即可。
现在需要让上面的text框成为不可编辑状态,我想了半天没想出办法,就想用自己的text框覆盖原来的text框,然后在用户点选一个文件时侯,时实得在自定义得text框中显示文件名,效果和原来jfilechooser一样,所以必须在用户点击"确定"和"取消"之前就得到文件名,实应该在选择事件中得到,不知道有没有这样得事件,请各位帮忙想想,谢先!!!!
ImageIcon thumbnail = null;
public FilePreviewer(JFileChooser fc) {
setPreferredSize(new Dimension(100, 50));
fc.addPropertyChangeListener(this); //*****//
setBorder(new BevelBorder(BevelBorder.LOWERED));
}
...
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) { //*****//
if(isShowing()) {
loadImage((File) e.getNewValue());
repaint();
}
}
}