LWJGL
May 22, 2012, 15:29:58 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL 2.8.3 released!
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: [solved] How can I retrieve a filename with the path included (/path/filename)?  (Read 1034 times)
Cottonwood
Regular nerd
**
Posts: 50



WWW
« on: December 23, 2010, 02:20:01 »

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?

Code:
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;
    }
}
Logged

Regards. Cottonwood.
Matthias
Talks Too Much
***
Posts: 178


WWW
« Reply #1 on: December 23, 2010, 02:27:24 »

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

Cottonwood
Regular nerd
**
Posts: 50



WWW
« Reply #2 on: December 23, 2010, 04:17:37 »

Yes, sorry. Related to java programming I'm just a beginner.  Undecided So sometimes I'm not able to see the wood for the trees.
Logged

Regards. Cottonwood.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!