简体   繁体   English

如何获取具有JTextPane所有属性的内容?

[英]How to get the content with all attributes of a JTextPane?

I have a JTextPane and on a ActionEvent , I want to save the content of the JTextPane (with the colors, text and other attributes) in another class (as a variable). 我有一个JTextPane并且在ActionEvent ,我想将JTextPane的内容(带有颜色,文本和其他属性)保存在另一个类中(作为变量)。 Later on I want to exchange the content of the JTextPane with the one of the variable. 稍后,我想与变量之一交换JTextPane的内容。

I have tried to use: 我尝试使用:

(StyledDocument)myTextPane.getDocument()

and

myTextPane.getStyledDocument()

but both didn't work. 但两者都不起作用。

I thought about just give the whole JTextPane over, but apperently the pane gets still updated in the other class... 我考虑过只将整个JTextPane移交给我,但是很显然,窗格仍在其他类中更新了……

EDIT: I would only use the content to show it (later) in the JTextPane again. 编辑:我只会使用内容再次在JTextPane显示它(以后)。 I won't be saving it in a file or something similar. 我不会将其保存在文件或类似文件中。

Actually it depends on EditorKit you use. 实际上,这取决于您使用的EditorKit。 Each kit has own format to store/load content. 每个套件都有自己的格式来存储/加载内容。

For the simplest case (eg if you use HTMLEditorKit) you can use getText()/setText(). 对于最简单的情况(例如,如果使用HTMLEditorKit),可以使用getText()/ setText()。

If you need own format and would like to store the content yourself read this 如果您需要自己的格式,并且想自己存储内容,请阅读内容

Also you can try to use the AdvancedRTFEditorKit to store the content as RTF (default RTFEditorKit is limited) 您也可以尝试使用AdvancedRTFEditorKit将内容存储为RTF(默认RTFEditorKit是有限的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM