简体   繁体   English

每当我想清除一个新的DefaultStyledDocument并重新开始时,是否要用这种不好的形式?

[英]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.

相关问题 将一个DefaultStyledDocument插入另一个DefaultStyledDocument - Insert one DefaultStyledDocument into another DefaultStyledDocument 如何保存字符串变量的值并在以后每次运行代码时使用它? - How to save a value of a String variable and use it later when i wish even every time i run the code? 每次打开新活动时,Android SQLite数据库似乎都会清除 - Android SQLite database seems to clear every time i open a new activity 每次启动服务器时如何停止Eclipse(STS)创建新的运行时配置 - How do I stop Eclipse (STS) from creating a new runtime configuration every time I start a server 每次播放TTS时如何将音频文件另存为新文件? - How can save the audio file as new one every time I play the tts? 每次 WildFly 启动时如何创建一个新的 server.log? - How to create a new server.log every time WildFly start? 如何开始新的活动并等待一段时间并开始另一个活动? - How to start new activity and wait some time and start another one? 每次在Windows中启动应用程序时如何记录日志? - How do I log every time I start an application in Windows? 每次创建一个新对象或保留一个 - Create a new object every time or keep a single one 每次我开始上一个活动时Android应用程序崩溃 - Android app crashes every time i start the previous activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM