2016-04-07 21 views
0

Dosyayı JFileChooser numaralı düğmeden kaldırmak istediğimde, bazı düğmeleri tıklatıyorum. Örneğin, "Sıfırla" düğmesini tıklatırsam, JFileChooser seçili dosya seçili olmaz. Burada Reset butonu koduSeçili dosya biçimini temizle 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(); 

Ve:

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(""); 

Teşekkür

İşte benim JFileChooser kod var.

+0

Kodunuzda birçok sorun var. Göstericiyi gösterdikten sonra yapılandırırsınız (showOpenDialog() öğesi chooser.getSelectedFile() öğesinden önceki sonuncudur). Filtre oluşturuyorsunuz ancak Dosya Seçici'de ayarlamıyorsunuz. – PhoneixS

cevap

1

Gerçekten de JFileChooser dosyasını silmek istemezsiniz, sınıfınızda bulunan dizgiyi sıfırlarsınız (ve bunun bir temsili, normalde JLabel'de). Ve dosya seçiciyi yeniden kullanmalısınız.

Sıfırlama yapmazsanız ve her seferinde yeniden oluşturmazsanız, kullanıcı genellikle iyi bir UX olan aynı dizini açar.

Basit bir örnek aşağıdaki gibi olabilir:

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 = ""; 
    } 

} 

Eğer JFileChooser seçilen dosyayı değiştirmek istiyorum sebebi ne olursa olsun JFileChooser.showSaveDialog(...) - how to set suggested file name bkz olmasaydı. Ayrıca How to Use File Choosers numaralı telefondan resmi eğiticiye bir göz atın.


Kodunuzdaki diğer sorunlar hakkındaki yorumlarımı görün.