jtable to image conversion not happening properly

后端 未结 3 1717
我寻月下人不归
我寻月下人不归 2021-01-15 13:05

I am trying to create a buffered image from a table, when i add the table to a application frame and set the size I am able to view it properly but when I convert it into an

相关标签:
3条回答
  • 2021-01-15 13:39

    I went through the post at this location Truncated JTable print output and included the answer at that location it seems to be working fine now.

    Thank you for your help

    0 讨论(0)
  • 2021-01-15 13:41

    There are a few issues I can see here.

    • You should use printAll rather than paint when drawing to the image.

    • The height of the table is set when the pack method is called, but this is before the table is populated. If you are creating the image immediately after generating the table then the size may not be set. You should populate the table before you pack the frame - that should ensure the size is available when you need it.

    EDIT: Following your code change I would suggest removing the scroll pane (as kleopatra suggested) and then using printAll. paint should not be used for images as it can use double buffering. To see the header you will need to set its size using the code provided by kleopatra. The header's size is independent of the table so calling setSize on the table does not alter the header.

    0 讨论(0)
  • 2021-01-15 13:44

    Basically, you have to do the layout work for all components you want to paint to the image, that is in this case both the table and the tableHeader, by setting their sizes (below the pref is used).

    BTW, adding to a JScrollPane is not helpful (as you have seen), except when the pane is realized - adding the header is done by the table in addNotify.

        JTable table = new JTable(new AncientSwingTeam());
        JTableHeader header =table.getTableHeader();
        table.setSize(table.getPreferredSize());
        header.setSize(header.getPreferredSize());
        int w = Math.max(table.getWidth(), header.getWidth());
        int h = table.getHeight() + header.getHeight();
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        header.paint(g2);
        g2.translate(0, header.getHeight());
        table.paint(g2);
        g2.dispose();
        JLabel label = new JLabel(new ImageIcon(bi));
        showInFrame(label, "image of table");
    

    Edit: comment on TextAreaRenderer

    The general rule is to never-ever-ever change the calling table in the renderer's getXXRendererComponent, the table given as parameter is to be regarded read-only, strictly. Breaking the rule can lead to ugly loops (setting a new rowHeight while painting triggers a new painting request) or artefacts (there is no guarantee when the renderer is called, so the correct rowheight might or not have been set) as you see here.

    Instead do the measuring somewhere outside. On detecting a change (f.i. in the model) which might lead to updating the sizes, walk the rows, measure all its cells and update to the largest. Simply extract all your sizing code from the renderer.

    0 讨论(0)
提交回复
热议问题