I am working on printing a series of JPanels on Printable , the main printing interface that provides a Graphics object that you draw what you want to print. If I have a live JPanel, that is, somewhere in the user interface, everything works fine.
However, if I create a JPanel and never add it to the user interface, printAll () does not seem to do anything. Code abbreviation to SSCCE:
import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SSCCEPaintInvisible { public static void main(String[] args) { JPanel panel = new JPanel();
If I create a JFrame for the panel and add the panel to the JFrame and make the JFrame visible before calling paintAll (), the code captures the user interface on the chart as expected. Of course, this means that the screen displays a JFrame for printing. A.
Is there a JPanel rendering method that has never been added to the user interface in a Graphics object? Thanks!
source share