在使用JFileChooser保存文件时的问题 这个有点像BCB里面的SaveDialog,BCB里面只要在SaveDialog的Option设置ofOverwritePrompt属性为true就可以的。我查遍了帮助都没有找到答案,郁闷了一整天了啊,有没有高手指点一下? 感谢感谢!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你到你的jdk目录下,有这个例子,我刚刚还试了一下,正好under the demo/jfc directory in the Java 2 SDK, Standard Edition.我正正在看源代码 可以自己写code实现吧。好像不难的。就是如果要写入的文件的文件名已经存在,那么就生成一个Dialog吧。 可是JFileChooser是被封装好的啊,弹出对话框时,已经在了类的方法里面了,实在想不到该从哪里切入啊?难道他就没有事先做好这个东西?我看了JDK的Sample里面的FileChooserDemo,原来他这个Demo也没有这样做,保存就直接保存了555 唉只好自己去实现它了 我自己实现了一个JFileChooser的派生类,重写了JFileChooser的approveSelection()方法。做完了以后想起来其实也很简单,可能是C++的后遗症吧,走了太多弯路以下是源码:-----------------------------------------package mytools.MyComponent;import javax.swing.JFileChooser;import javax.swing.JOptionPane;import javax.swing.filechooser.FileSystemView;import java.io.File;public class MyFileChooser extends JFileChooser { public MyFileChooser() { super(); } public MyFileChooser(File currentDirectory) { super(currentDirectory); } public MyFileChooser(File currentDirectory, FileSystemView fsv) { super(currentDirectory, fsv); } public MyFileChooser(FileSystemView fsv) { super(fsv); } public MyFileChooser(String currentDirectoryPath) { super(currentDirectoryPath); } public MyFileChooser(String currentDirectoryPath, FileSystemView fsv) { super(currentDirectoryPath, fsv); } public void approveSelection() { if(this.getDialogType()==JFileChooser.SAVE_DIALOG) { File temp = this.getSelectedFile(); if(temp.exists()) { if( JOptionPane.showConfirmDialog(this, temp.getPath()+" 已存在。\n要替换它吗?", "另存为", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.NO_OPTION ) return; } } super.approveSelection(); }}-----------------------------------------我把它打包成自己的一个库了,里面还包括一些自己做的组件:下载请到 http://www.coocie.dns2go.com/bbs/index.php?s=50583cadb9052275a081f77d78caa219&act=ST&f=1&t=121【mytool.MyComponent.MyFileChooser】就是上面那个,使用时只要这样改改就行了噢JFileChooser = jFileChoooser1 = new JFileChooser(); ~~~~~~~~~~~~~~\ 这里改成MyFileChooser()当然还要设置一下这个库的路径,然后import...就不多说啦【mytool.MyComponent.ExampleFileFilter】FileFilter的子类,实现了accept(File f),getExtension(File f),addExtension(String extension),getDescription(),setDescription(String description),setExtensionListInDescription(boolean b),isExtensionListInDescription()等方法,以后大家使用JFileChooser要过滤文件时,直接用它就可以了 ...... ExampleFileFilter filter = new ExampleFileFilter(); filter.addExtension("txt"); filter.setDescription("文本文件"); jFileChooser1.setFileFilter(filter); ......PS:咔咔,其中很多都是参考了JDK1.4里面的Demo【mytool.MyComponent.MyUpDown】实现了一个类似BCB里面TUpDown组件,可以通过setMinValue()和setMaxValue()来设置范围,有数字检查,范围检查等,有兴趣的可以自己用用看啦:)555~~ 找遍了也没有发现Swing里有UpDown组件,只好自己动手啦【mytools.util.AbstractList.VectorQueue】这个不用说啦,一个队列 BufferedReader.mark方法参数问题 简单问题 怎样让程序一启动,就自动显示“关于...”对话框? 这个IF语句怎么老是只执行一个分支 如何将jbuilder中的项目导出整个项目的UML图 请教一个小程序 (急) void static public摆放有顺序要求吗? 请问:java怎样访问COM组件 为什么我的小程序用appletviewer可以运行,而在浏览器中就不行呢? 请帮忙:Bean里可以用session吗?如何用?有例程吗? 多谢你一万遍!!!! 怎样用applet修改jtable中的数据?要得到选中的项吗? 如何改变jTable中任意单元格的颜色help急
under the demo/jfc directory in the Java 2 SDK, Standard Edition.我正正在看源代码
我自己实现了一个JFileChooser的派生类,重写了JFileChooser的approveSelection()方法。做完了以后想起来其实也很简单,可能是C++的后遗症吧,走了太多弯路以下是源码:
-----------------------------------------
package mytools.MyComponent;import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileSystemView;
import java.io.File;public class MyFileChooser extends JFileChooser { public MyFileChooser() {
super();
} public MyFileChooser(File currentDirectory) {
super(currentDirectory);
} public MyFileChooser(File currentDirectory, FileSystemView fsv) {
super(currentDirectory, fsv);
} public MyFileChooser(FileSystemView fsv) {
super(fsv);
} public MyFileChooser(String currentDirectoryPath) {
super(currentDirectoryPath);
} public MyFileChooser(String currentDirectoryPath, FileSystemView fsv) {
super(currentDirectoryPath, fsv);
} public void approveSelection() {
if(this.getDialogType()==JFileChooser.SAVE_DIALOG) {
File temp = this.getSelectedFile();
if(temp.exists()) {
if( JOptionPane.showConfirmDialog(this,
temp.getPath()+" 已存在。\n要替换它吗?",
"另存为",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE)
== JOptionPane.NO_OPTION )
return;
}
}
super.approveSelection();
}
}-----------------------------------------我把它打包成自己的一个库了,里面还包括一些自己做的组件:
下载请到
http://www.coocie.dns2go.com/bbs/index.php?s=50583cadb9052275a081f77d78caa219&act=ST&f=1&t=121【mytool.MyComponent.MyFileChooser】
就是上面那个,使用时只要这样改改就行了噢
JFileChooser = jFileChoooser1 = new JFileChooser();
~~~~~~~~~~~~~~\
这里改成MyFileChooser()
当然还要设置一下这个库的路径,然后import...就不多说啦【mytool.MyComponent.ExampleFileFilter】
FileFilter的子类,实现了accept(File f),getExtension(File f),addExtension(String extension),getDescription(),setDescription(String description),setExtensionListInDescription(boolean b),isExtensionListInDescription()等方法,以后大家使用JFileChooser要过滤文件时,直接用它就可以了
......
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("txt");
filter.setDescription("文本文件");
jFileChooser1.setFileFilter(filter);
......
PS:咔咔,其中很多都是参考了JDK1.4里面的Demo【mytool.MyComponent.MyUpDown】
实现了一个类似BCB里面TUpDown组件,可以通过setMinValue()和setMaxValue()来设置范围,有数字检查,范围检查等,有兴趣的可以自己用用看啦:)
555~~ 找遍了也没有发现Swing里有UpDown组件,只好自己动手啦【mytools.util.AbstractList.VectorQueue】
这个不用说啦,一个队列