簡體   English   中英

DefaultStyledDocument.styleChanged(Style style)可能無法及時運行?

[英]DefaultStyledDocument.styleChanged(Style style) may not run in a timely manner?

我遇到了一個擴展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)的事實是否意味着我無法依靠呈現在文檔上的格式更改進行渲染?

如果是這種情況,是否有辦法確保在更新發生之前不進行渲染?

您會看到fireChangedUpdate(dde); 在代碼末尾。 嘗試將自己追加為DocumentListener DocumentListener.changedUpdate方法內部,應保存您的內容以打印包含所有更改的文檔。

我有類似的問題。

為了解決這個問題,我在設置了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.
  }
});

很恐怖,但是很有效,抱歉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM