これを使用してフィルタを追加します。例:
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.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);
int returnVal = chooser.showSaveDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
これは、JPGとGIFファイルのみを表示します。あなたは、ファイルの名前を知っているので、あなただけのオープンというボタンがあり、Runtime .getRuntimeを使用することができます:あなたはExampleFileFilterがabstactクラスFileFilter
編集を実装して知っているだけので:here
編集から盗まれた例。 exec( '開かれるファイル.doc')、適切なアプリケーションで開く必要があります。
保存するには、保存する場所を尋ねるメッセージが表示されます。 JFileChooser。私はまだフィルタを使い、必要に応じてファイル拡張子を動的に決定してから、次のようにします。
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.
String selectedFile = "The suggested save name.";
chooser.setSelectedFile(selectedFile);
ExampleFileFilter filter = new ExampleFileFilter();
String extension = "Do something to find your extension";
filter.addExtension(extension);
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);
int returnVal = chooser.showSaveDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
//then write your code to write to disk
}
希望します。
私はファイルをopenignに制限したくないのですが、ユーザーが特定のファイルを開くか保存するダイアログボックスを持っていたいと思っています。私のsystem.Hopeによって生成されたレポート今すぐクリアしてください。 – NewToJava
名前を知っている特定のファイルを開くように促したいと言っていますか? –
はいu r absolutly rit..wat私が望むのは、Gmailにアタッチメントがある場合、ダウンロードを促し、保存または開くのいずれかを示す方法です。 – NewToJava