-
Culturally dependent information in some documents is handled through a mechanism called character encoding. Character encoding is an unambiguous mapping of the members of a character set (letters, ideographs, digits, symbols, or control functions) to specific numeric code values. It represents the way the file is stored. Example character encodings are ISO-8859-1, ISO-8859-5, Shift-jis, Euc-jp, and UTF-8. When the file is passed to an user agent (
JEditorPane) it is converted to the document character set (ISO-10646 aka Unicode).There are multiple ways to get a character set mapping to happen with
JEditorPane.- One way is to specify the character set as a parameter of the MIME type. This will be established by a call to the
setContentTypemethod. If the content is loaded by thesetPagemethod the content type will have been set according to the specification of the URL. It the file is loaded directly, the content type would be expected to have been set prior to loading. - Another way the character set can be specified is in the document itself. This requires reading the document prior to determining the character set that is desired. To handle this, it is expected that the
EditorKit.read operation throw aChangedCharSetExceptionwhich will be caught. The read is then restarted with a new Reader that uses the character set specified in theChangedCharSetException(which is anIOException).
- One way is to specify the character set as a parameter of the MIME type. This will be established by a call to the
-
setContentType
public final void setContentType(String type)
Sets the type of content that this editor handles. This callsgetEditorKitForContentType, and thensetEditorKitif an editor kit can be successfully located. This is mostly convenience method that can be used as an alternative to callingsetEditorKitdirectly.If there is a charset definition specified as a parameter of the content type specification, it will be used when loading input streams using the associated
EditorKit. For example if the type is specified astext/html; charset=EUC-JPthe content will be loaded using theEditorKitregistered fortext/htmland the Reader provided to theEditorKitto load unicode into the document will use theEUC-JPcharset for translating to unicode. If the type is not recognized, the content will be loaded using theEditorKitregistered for plain text,text/plain.- Parameters:
type- the non-nullmime type for the content editing support- Throws:
NullPointerException- if thetypeparameter isnull- See Also:
getContentType()- 所以总之就是调用setContentType("text/html;charset=utf-8");即可。