[solved] How can I retrieve a filename with the path included (/path/filename)?

Started by Cottonwood, December 23, 2010, 10:20:01

Previous topic - Next topic

Cottonwood

When I need a filename to open the file I also need the path. Otherwise I can't open the file. But I only get fhe filename. So what please am I doing wrong?

import java.io.*;
import javax.swing.*;

public class ChooseFile extends JPanel{

    private static final long serialVersionUID = 1L;
    static JFrame frame = new JFrame("ChooseFile");
    static String filename;
    static String language = System.getProperty("user.language"), t1;
    JButton openButton, saveButton;
    static JFileChooser fc = new JFileChooser();

    public static String select(String fname) {
        filename = fname;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new ChooseFile());
        frame.setSize(500,100); 
        if (language == "de"){
            t1="Die Standard-Datei\""+filename+"\" wurde nicht gefunden.";
        }else{
            t1="The standard file \""+filename+"\" couldn't be found.";
        }
        frame.setTitle(t1);
        frame.setVisible(true);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnVal = fc.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            filename = file.getName();
            t1="Opening: " + filename + ".";
            if (language == "de"){
                t1="Öffne: " + filename + ".";
            }else{
                t1="Opening: " + filename + ".";
            }
            frame.setTitle(t1);
        } else {
            t1="Open command cancelled by user.";
            if (language == "de"){
                t1="Öffnen-Dialog: Abbruch durch den Benutzer.";
            }else{
                t1="Open dialog cancelled by user.";
            }
            frame.setTitle(t1);
            filename="";
        }
        try {Thread.sleep(4000);} catch(InterruptedException e) {}
        frame.setVisible(false);
        return filename;
    }
}
Regards. Cottonwood.

Matthias

Have you thought about just using the File object directly? It contains everything - even the path. Reading the Javadoc should have revealed that.

Cottonwood

Yes, sorry. Related to java programming I'm just a beginner.  :-\ So sometimes I'm not able to see the wood for the trees.
Regards. Cottonwood.