从 JEditorPane 打印

Pet*_*Mmm 2 java printing swing

(我重写了原来的问题。问题是一样的。)

上面的示例代码不打印图像。它出现在窗口中但不打印。

public static void main(String[] args)  {

        final JEditorPane ed = new JEditorPane(
                "text/html",
                "<p>Test<br><img  src='http://www.google.es/images/logos/ps_logo2.png'></p>");

        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        f.add(ed);

        JButton b = new JButton("Print");
        f.add(b,BorderLayout.SOUTH);
        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                try {
                    ed.print();
                } catch (PrinterException ex) {
                    System.err.println(ex);
                }
            }
        });

        f.pack();
        f.setVisible(true);
    }
Run Code Online (Sandbox Code Playgroud)

Pet*_*Mmm 5

现在得到了这个。“秘诀”就是拦截图片加载过程,提示图片应该同步加载。

ed.setEditorKit(new HTMLEditorKit() {

            @Override
            public ViewFactory getViewFactory() {
                return new HTMLFactory() {

                    @Override
                    public View create(Element elem) {
                        View view = super.create(elem);
                        if (view instanceof ImageView) {
                            ((ImageView) view).setLoadsSynchronously(true);
                        }
                        return view;
                    }
                };
            }
        });
Run Code Online (Sandbox Code Playgroud)