2016-04-07 11 views
0

ボタンをクリックするとファイルをJFileChooserから選択解除します。 たとえば、「リセット」ボタンをクリックすると、選択したファイルJFileChooserが選択解除されます。ここで選択したファイル形式を消去するJFileChooser

は私JFileChooserのコードです:

public void fileChoose(){ 
    JFileChooser chooser = new JFileChooser(); 
    chooser.showOpenDialog(null); 
    chooser.setCurrentDirectory(new File(System.getProperty("user","home"))); 
    FileNameExtensionFilter filter = new FileNameExtensionFilter("jpg", "png"); 
    File file = chooser.getSelectedFile(); 
    String path = file.getAbsolutePath(); 

そして、ここでリセットボタンコード:

private void clearAllField(){ 
    nik_input.setText(""); 
    name_input.setText(""); 
    born_input.setText(""); 
    birth_date_input.setDate(null); 
    gender_input.setSelectedIndex(0); 
    address_input.setText(""); 
    job_input.setText(""); 

感謝。

+0

コードには多くの問題があります。ダイアログを表示した後で設定します(showOpenDialog()がchooser.getSelectedFile()の前の最後になるように順序を変更します)。あなたはフィルタを作成しますが、ファイルセレクタでフィルタを設定していません。 – PhoneixS

答えて

1

あなたは本当にJFileChooserのファイルをクリアしたくはありません。クラスにある文字列(およびその表現、通常はJLabel)をリセットします。そして、ファイルチューザーを再利用する必要があります。

毎回リセットしないで再作成しないと、ユーザーには同じディレクトリが開かれます。これは通常、良いUXです。

簡単な例は次のように考えられます。

public class Foo { 

    JFileChooser chooser; 
    String path; 

    public Foo() { 
    this.chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new File(System.getProperty("user","home"))); 
    // TODO Other file chooser configuration... 
    path = ""; 
    } 

    public void fileChoose(){ 

    chooser.showOpenDialog(null); 
    File file = chooser.getSelectedFile(); 
    this.path = file.getAbsolutePath(); 

    } 

    public String getPath() { 
    return this.path; 
    } 

    public String resetPath() { 
    this.path = ""; 
    } 

} 

何らかの理由であなたがJFileChooserの中に、選択したファイルJFileChooser.showSaveDialog(...) - how to set suggested file nameを参照を変更したい場合。

How to Use File Choosersの公式チュートリアルもご覧ください。


コードのその他の問題についてのコメントを参照してください。