【问题标题】:How to add background image in JTable when printing?打印时如何在JTable中添加背景图像?
【发布时间】:2011-10-23 19:21:10
【问题描述】:

我想打印带有背景图像或水印的JTable。我的代码是:

public void actionPerformed(ActionEvent ae)
{
    boolean status=false;

    MessageFormat header = null;
    header = new MessageFormat("Header");
    MessageFormat footer = null;
    footer = new MessageFormat("Page");

    boolean fitWidth = true;
    boolean showPrintDialog = true;
    boolean interactive = true;

    /* determine the print mode */
    JTable.PrintMode mode = fitWidth ? JTable.PrintMode.FIT_WIDTH
                                     : JTable.PrintMode.NORMAL;

    try
    {
        status = jt.print(mode, header, footer,showPrintDialog,null,interactive);

        if(status ==true)
        {
            frame.dispose();
        }
    }
    catch(Exception ee)
    {
        System.out.println(ee.getMessage());
    }
}

如何在这个方法中传递或设置背景图片?

【问题讨论】:

    标签: java swing jtable


    【解决方案1】:

    没有简单的方法为整个JTable 设置背景,但是JViewPort 来自JScrollPane 可以轻松做到这一点,那么不管是在JScrollPane 内部还是JTable 或另一个@987654324 @

    例如

    import java.awt.*;
    import javax.swing.*;
    
    class ImageAsTableBackround {
    
        private JScrollPane sp;
        private JTable table;
        private String[] head = {"One", "Two", "Three", "Four", "Five", "Six"};
        private String[][] data = new String[25][6];
    
        public void buildGUI() {
            sp = new JScrollPane();
    
    // uncomment these codes lines for panting an image from package, 
    // but then block code table = new TableBackroundPaint0(data, head);
            //sp.setViewport(new ImageViewport());
            //table = new JTable(data, head);
            //table.setOpaque(false);
    
    // code for painting from generated code    
            table = new TableBackroundPaint0(data, head);
    
            table.setBackground(new Color(0, 0, 0, 0));
            table.setFillsViewportHeight(true);
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            sp.setViewportView(table);
            JFrame frame = new JFrame();
            frame.getContentPane().add(sp);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        class ImageViewport extends JViewport {
    
            private static final long serialVersionUID = 1L;
            private Image img;
    
            public ImageViewport() {
                try {
                    ImageIcon image = new ImageIcon(getClass().getResource("resources/PICT6090.jpg"));
                    img = image.getImage();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (img != null) {
                    g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
                } else {
                    g.drawString("This space for rent", 50, 50);
                }
            }
        }
    
        class TableBackroundPaint0 extends JTable {
    
            private static final long serialVersionUID = 1L;
    
            TableBackroundPaint0(Object[][] data, Object[] head) {
                super(data, head);
                setOpaque(false);
                ((JComponent) getDefaultRenderer(Object.class)).setOpaque(false);
            }
    
            @Override
            public void paintComponent(Graphics g) {
                Color background = new Color(168, 210, 241);
                Color controlColor = new Color(230, 240, 230);
                int width = getWidth();
                int height = getHeight();
                Graphics2D g2 = (Graphics2D) g;
                Paint oldPaint = g2.getPaint();
                g2.setPaint(new GradientPaint(0, 0, background, width, 0, controlColor));
                g2.fillRect(0, 0, width, height);
                g2.setPaint(oldPaint);
                for (int row : getSelectedRows()) {
                    Rectangle start = getCellRect(row, 0, true);
                    Rectangle end = getCellRect(row, getColumnCount() - 1, true);
                    g2.setPaint(new GradientPaint(start.x, 0, controlColor, (int) ((end.x + end.width - start.x) * 1.25), 0, Color.orange));
                    g2.fillRect(start.x, start.y, end.x + end.width - start.x, start.height);
                }
                super.paintComponent(g);
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new ImageAsTableBackround().buildGUI();
                }
            });
        }
    }
    

    【讨论】:

    • getResource("resources/some.jpg") 为什么不在源代码中生成简单的图像,或者热链接到网络上的图像?
    • +1 相关示例herehere
    • RotatableImage 是一个方便的合成图像,但这个checkerboard 也会很有趣。
    • @Andrew Thompson 你是对的,ehrrrgggt,好主意,同意,接受和修改
    【解决方案2】:

    问题有两个部分

    • 打印每页一张图片
    • 确保图像“透过”桌子

    @mKorbel 解决了第二个问题(虽然没有真正解决,因为没有好的解决方案 :-)

    为了解决第一个问题,我会使用自定义 Printable 和子类 JTable 来返回它,类似于

    public class BackgroundPrintable implements Printable {
    
        Printable tablePrintable;
        JTable table;
        MessageFormat header; 
        MessageFormat footer;
        BufferedImage background;
    
        public BackgroundPrintable(MessageFormat header, MessageFormat footer) {
            this.header = header;
            this.footer = footer;
        }
    
        public void setTablePrintable(JTable table, Printable printable) {
            tablePrintable = printable;        
            this.table = table;
        }
    
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, 
                int pageIndex) throws PrinterException {
            printImage(graphics, pageFormat, pageIndex);
            int exists = tablePrintable.print(graphics, pageFormat, pageIndex);
            if (exists != PAGE_EXISTS) {
                return exists;
            }
            return PAGE_EXISTS;        
        }
    
        private void printImage(Graphics graphics, PageFormat pageFormat,
                int pageIndex) {
            // grab an untainted graphics
            Graphics2D g2d = (Graphics2D)graphics.create();
            // do the image painting 
            ....
            // cleanup  
            g2d.dispose();
        }
    
    }
    
    // use in JTable subclass
    @Override
    public Printable getPrintable(PrintMode printMode,
            MessageFormat headerFormat, MessageFormat footerFormat) {
        Printable printable = super.getPrintable(printMode, null, null);
        BackgroundPrintable custom = new BackgroundPrintable(headerFormat, footerFormat);
        custom.setTablePrintable(this, printable);
        return custom;
    }
    

    要实现第二个,JTable 及其渲染器都必须是透明的。棘手,因为:

    • 可能仅在打印时 - 否则它们应该具有通常的不透明度
    • 所有渲染器都必须是透明的,没有完全安全的方法来获取所有渲染器

    自定义 JTable 可以尝试通过在其 prepareRenderer 中强制呈现组件的不透明度来实现:

      @Override
       public Component prepareRenderer(TableCellRenderer renderer,
                int row, int column) {
            JComponent comp = (JComponent) super.prepareRenderer(renderer, row, column);
            if (isPaintingForPrint()) {
                comp.setOpaque(false);
            } else {
                comp.setOpaque(true);
            }   
            return comp;
       }
    

    实际上,这并不完全有效:else 块中的代码对于自然透明的组件可能是错误的做法。恐怕没有真正可靠的解决方案。

    【讨论】:

    • 感谢您的想法,对于打印我总是使用带有 JTable 和共享 TableModel 的不可见容器,非常适合实验 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2017-01-27
    • 1970-01-01
    • 2018-06-03
    • 2010-10-10
    • 2012-06-29
    • 2011-02-23
    相关资源
    最近更新 更多