Question

i created an html editor and i want to get the file name & path of opened file in the JTextPane. any suggestion?

Was it helpful?

Solution

Assuming you make use of the File chooser (the file picker), which seems quite likely for a code editor you could simply save the file path you receive as a result:

public void actionPerformed(ActionEvent e) {
    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(FileChooserDemo.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //At this point you can use: file.getName() to get your filename
            //You can also use file.getPath()
        } else {
            //Canceled opening
        }
    }
}

You could save the result of file.getName() and file.getPath() to a string that you would assign to your JTextPane later on.

For additional information on file choosers see the documentation which also explains this process in more detail.

Should you be working with File you can make use of the same functions which will provide the same information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top