Pregunta

Estoy experimentando un problema intermitente con una clase que se extiende javax.swing.text.DefaultStyledDocument. Este documento se envía a una impresora. La mayoría de las veces el formato del documento parece correcto, pero de vez en cuando no es así. Parece que algunos de los cambios en el formato no se han aplicado.

Me tomó un vistazo al código 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();
    }
}
}

¿El hecho de que SwingUtilities.invokeLater(updateRunnable) se llama, en lugar de invokeAndWait(updateRunnable), significa que no puedo contar con mis cambios de formato que aparecen en el documento antes de que se hace?

Si ese es el caso, ¿hay una manera de asegurar que no proceda con la prestación hasta que se han producido los cambios?

¿Fue útil?

Solución

Usted ve un fireChangedUpdate(dde); al final del código. Intentar añadir a sí mismo como un DocumentListener. Dentro del método DocumentListener.changedUpdate podria estar a salvo para imprimir el documento con todos los cambios incluidos.

Otros consejos

he tenido un problema similar.

Para resolv, que se ejecuta tras conjunto algo en un texto columpio, un invokeLater vacía, y cuando se hace de esta invokeLater, espero que el texto oscilación de invocación que se hace más adelante.

Mi código es quizás mejor que mi Inglés:

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.
  }
});

Es horribel, pero es un trabajo, lo siento.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top