我遇到了一个扩展javax.swing.text.DefaultStyledDocument的类的间歇性问题。正在将此文档发送到打印机。大多数情况下,文档的格式看起来是正确的,但有时不正确。看起来格式中的某些更改尚未应用。
我看了一下DefaultStyledDocument.styleChanged(Style style)代码:
/**
* Called when any of this document's styles have changed.
* Subclasses may wish to be intelligent about what gets damaged.
*
* @param style The Style that has changed.
*/
protected void styleChanged(Style style) {
// Only propagate change updated if have content
if (getLength() != 0) {
// lazily create a ChangeUpdateRunnable
if (updateRunnable == null) {
updateRunnable = new ChangeUpdateRunnable();
}
// We may get a whole batch of these at once, so only
// queue the runnable if it is not already pending
synchronized(updateRunnable) {
if (!updateRunnable.isPending) {
SwingUtilities.invokeLater(updateRunnable);
updateRunnable.isPending = true;
}
}
}
}
/**
* When run this creates a change event for the complete document
* and fires it.
*/
class ChangeUpdateRunnable implements Runnable {
boolean isPending = false;
public void run() {
synchronized(this) {
isPending = false;
}
try {
writeLock();
DefaultDocumentEvent dde = new DefaultDocumentEvent(0,
getLength(),
DocumentEvent.EventType.CHANGE);
dde.end();
fireChangedUpdate(dde);
} finally {
writeUnlock();
}
}
}调用的是SwingUtilities.invokeLater(updateRunnable)而不是invokeAndWait(updateRunnable),这是否意味着我不能指望在呈现文档之前我的格式更改就会出现在文档中?
如果是这样的话,有没有办法确保我在更新之前不会继续渲染?
发布于 2010-05-10 23:51:47
您将在代码的末尾看到一个fireChangedUpdate(dde);。尝试将自己附加为DocumentListener。在DocumentListener.changedUpdate方法中,您应该保存以打印包含所有更改的文档。
发布于 2010-05-07 05:14:00
我也遇到过类似的问题。
为了解析,我在swing文本中设置了一些内容后启动,一个空的invokeLater,当这个invokeLater完成时,我希望稍后的swing文本调用也完成。
我的代码可能比我的英语更好:
doc.formatSomethingWhichPerhapsLaunchInvokeLater();
EventQueue.invokeLater(new java.lang.Runnable()
{
public void run()
{
// at this point, I hope all swing text stuff is finish.
// Until now, it's the case.
}
});很可怕,但这是工作,抱歉。
https://stackoverflow.com/questions/2784420
复制相似问题