Using Java swing print ()

Does jaw swing print () need to be called on EDT (event dispatch stream)?

It takes a long time to complete, and working on EDT for a long time is a pain, as we all know.

+4
source share
2 answers

The short answer is no, printing should not occur on EDT.

This is described in the official textbook: How to print text

Printing interactively or non-interactively

In interactive mode, a progress dialog with the cancellation option is displayed for the entire printing period. Here is an example of a progress dialogue.

This dialog allows the user to track the progress of printing. The progress dialog is modal when the print method is called in the event dispatch stream and in a modeless image. It is important that your document remains unchanged when printing, otherwise the printing behavior is undefined. The printing method ensures that your document is not modified and will disable the component for the duration of printing. A.

If you call the print method in the non-interactive mode of sending events, then all events, including repaints, will be blocked. That's why printing non-interactively on EDT is only recommended for applications with an invisible graphical interface.

+3
source

Remember that JTextComponent is the only Swing component that can be printed in the background stream using its special printing methods. The printing method inherited from JComponent is not secure.

In general: Swing has a single-threaded thread, unless otherwise specified. The JTextComponent special printing method explicitly indicates that it is thread safe:

http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#print(java.text.MessageFormat,%20java.text.MessageFormat,%20boolean,%20javax.print .PrintService,% 20javax.print.attribute.PrintRequestAttributeSet,% 20boolean)

+1
source

Source: https://habr.com/ru/post/1339212/


All Articles