How to use html tags in JTextArea

橙三吉。 提交于 2019-12-04 19:08:10

问题


When I try to change color of a JTextArea using

textArea.setText("<html> <font color=\"red\"> Hi </font></html>")

, the text is shown is basically the whole text written inside setText method. But similar things work for JLabel, JButton etc.

How can I do that for a JTextArea also?


回答1:


  • don't to use JTextArea for Html formatted Object (even is possible, but why bothering)

  • use JTextPane / JEditorPane instead (setContentType("text/html"))

  • notice, todays Java and methods implemented in Official API by Oracle supporting only reduced Html syntax with css <= Html 3.2

  • use JavaFX in the case that you want/need to use todays Html5,




回答2:


JavaFX WebView is very powerful for rendering HTML and You can easily display javafx component(WebView) in your Swing Application by using JFXPanel. For tutorial of embedding fx in swing here is one from oracle: http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm

Thanks



来源:https://stackoverflow.com/questions/13195131/how-to-use-html-tags-in-jtextarea

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