[英]Is it bad form to build a new DefaultStyledDocument every time I wish to clear one and start anew?
My program ends up taking a StyledDocument
object from one JTextPane
(A) and passing it off to another JTextPane
(B). 我的程序最终从一个JTextPane
(A)获取一个StyledDocument
对象,并将其传递给另一个JTextPane
(B)。 When I have finished passing, I wish for the JTextPane
(A) to be clear of text and any formatting and basically be a fresh build of the object with its default JTextPane
settings. 完成传递之后,我希望JTextPane
(A)清除文本和任何格式,并且基本上是使用默认JTextPane
设置重新构建对象。 To do this, I am currently doing something like: 为此,我目前正在做类似的事情:
//make things
JTextPane inputField = new JTextPane();
JTextPane outputField = new JTextPane();
//move inputField text (with formatting) from inputField to outputField
StyledDocument doc = inputField.getStyledDocument();
EditorKit kit = inputField.getEditorKit();
outputField.setStyledDocument(doc);
outputField.setEditorKit(kit);
outputField.revalidate();
//reset the inputField so that it's fresh and ready for new input
inputField.setStyledDocument(new DefaultStyledDocument());
inputField.setEditorKit(new StyledEditorKit());
I realize in this example I don't have any text or formatting being moved(just a blank document object), but those are the operations I am performing, and am curious if "newing" a kit and document are a lazy way to reset my JTextPane
to default settings. 我意识到在此示例中,我没有任何文本或格式被移动(只是一个空白文档对象),但是这些是我正在执行的操作,并且很好奇“更新”工具包和文档是否是一种懒惰的重置方式我的JTextPane
为默认设置。 Thanks in advance! 提前致谢!
IMHO it's absolutely fine to create a new instance of document. 恕我直言,创建文档的新实例是绝对好的。 In fact it's faster because listeners don't update views to reflect empty Document
and then the new Document's
content. 实际上,它更快,因为侦听器不会更新视图以反映空Document
,然后反映新Document's
内容。
BTW: no need to reset kit if it's the same class. 顺便说一句:如果是同一类,则无需重置套件。 It's enough to call setDocument()
调用setDocument()
足够了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.