DefaultStyledDocument.styleChanged(スタイルスタイル)はタイムリーに実行されない場合がありますか?
-
03-10-2019 - |
質問
私は拡張するクラスで断続的な問題を経験しています 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)
, 、ドキュメントがレンダリングされる前に、ドキュメントに表示されるフォーマットの変更を期待できないということですか?
その場合、更新が発生するまでレンダリングを進めないようにする方法はありますか?
解決
あなたはAを見ます fireChangedUpdate(dde);
コードの最後に。自分自身をaとして追加してみてください DocumentListener
. 。内側 DocumentListener.changedUpdate
メソッドを保存する必要があります。すべての変更を含むドキュメントを印刷する必要があります。
他のヒント
同様の問題がありました。
Resolvに、私はスイングテキスト、空のInvokelaterで何かを設定した後に起動し、このInvokelaterが完了したら、後でスイングテキストが完了することを願っています。
私のコードはおそらく私の英語よりも優れています:
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.
}
});
それは恐ろしいですが、それは仕事です、ごめんなさい。