Is there a way to attatch a css file to a jEditorPane?

你说的曾经没有我的故事 提交于 2019-12-19 19:59:25

问题


Simple enough question: I have a string containing HTML that is being handed off to a JEditorPane for user consumption.

Can I attach a CSS file (or string containing CSS rules) to allow for more specific styling of the text?


回答1:


The HTMLEditorKit per default looks for a file default.css - I'm not sure where, though.

Alternatively, this should work:

StyleSheet ss = new StyleSheet();
ss.importStyleSheet(styleSheetURL);
HTMLEditorKit kit = (HTMLEditorKit)jEditorPane.getEditorKit();
kit.setStyleSheet(ss);

However, note that HTMLEditorKit only supports a limited subset of CSS 1.




回答2:


Can't you just include a style tag along with the HTML content in setText()?

e.g.

jEditorPane.setText( "<html><head><style type=\"text/css\">...</style></head><body>...");


来源:https://stackoverflow.com/questions/1436120/is-there-a-way-to-attatch-a-css-file-to-a-jeditorpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!