0

I have a JEditorPane in my application and I was loading java files into it using jsyntaxpane and the following code and it was working perfectly:

to highlight

jsyntaxpane.DefaultSyntaxKit.initKit();
textarea.setContentType("text/java");

to load file in

int a = filesToCompileList.getSelectedIndex();
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
textarea.read.read(br, index);
br.close();
textarea.requestFocus();

but know i had to change the way i was loading the file in and i am currently loading the files in like

File file = new File(filePath);
textarea.setPage(file.toURI().toURL());

this is loading the files in the way i want but isn't highlighting the text for java files anymore! does anybody know how i can fix this or get java highlighting a different way?

flexinIT
  • 431
  • 2
  • 10
  • 28

1 Answers1

0

AFAIK, it can not be done if you don't implement your own version of the jeditorpane. From the javadoc

The setPage method can be used to initialize the component from a URL. In this case, the content type will be determined from the URL, and the registered EditorKit for that content type will be set.

So, the mime type of the content will be inherit from the mime type of the url. Call setContentyType later will have no effect, as this will change the model of the jeditorpane, cleaning the content. Again from the Javadoc

NOTE: This has the side effect of changing the model, because the EditorKit is the source of how a particular type of content is modeled. This method will cause setDocument to be called on behalf of the caller to ensure integrity of the internal state.

So you must keep using the read method.

Francisco Puga
  • 23,869
  • 5
  • 48
  • 64