Search code examples
javaswingfile-iojtreejeditorpane

Adding file to JEditorPane using Full Directory Path


I have been searching for this for awhile now online and I have not been able to find any real information on it... I was wondering is there anyway to open up a file(like a java file) from using the files full directory path?

I am currently add files to my JEditorPane using

FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
jEditorPane.read(br, indexOfFile); 

which is ok but it makes it alot more complicated to add files this way as i have chenged the design of my application to have a JTree and i am not able to get the index of the files correctly now!

So is there a way to add a file to a JEditorPane using the files path?


Solution

  • Define a static method once and use it everywhere:

    public static void loadTextFileIntoEditorPane(String filePath, JEditorPane editor) throws IOException
    {
        File file = new File(filePath);
        editor.setPage(file.toURI().toURL());
    }