Java Utililty Methods JTable Screenshot

List of utility methods to do JTable Screenshot

Description

The list of methods to do JTable Screenshot are organized into topic(s).

Method

BufferedImagecreateTableScreenshot(Component component)
create Table Screenshot
Component source;
Dimension sourceSize;
JTable table;
if (component instanceof JTable) {
    table = (JTable) component;
    if ((table.getTableHeader() == null) || !table.getTableHeader().isVisible()) {
        return createGeneralComponentScreenshot(component);
    source = table;
    sourceSize = table.getSize();
} else if (component instanceof JViewport && ((JViewport) component).getView() instanceof JTable) {
    JViewport viewport = (JViewport) component;
    table = (JTable) viewport.getView();
    if ((table.getTableHeader() == null) || !table.getTableHeader().isVisible()) {
        return createGeneralComponentScreenshot(component);
    if (table.getSize().height > viewport.getSize().height) {
        source = viewport;
        sourceSize = viewport.getSize();
    } else {
        source = table;
        sourceSize = table.getSize();
} else {
    throw new IllegalArgumentException("Component can only be JTable or JViewport holding JTable"); 
final JTableHeader tableHeader = table.getTableHeader();
Dimension tableHeaderSize = tableHeader.getSize();
BufferedImage tableScreenshot = new BufferedImage(sourceSize.width,
        tableHeaderSize.height + sourceSize.height, BufferedImage.TYPE_INT_RGB);
Graphics tableScreenshotGraphics = tableScreenshot.getGraphics();
tableHeader.printAll(tableScreenshotGraphics);
tableScreenshotGraphics.translate(0, tableHeaderSize.height);
source.printAll(tableScreenshotGraphics);
return tableScreenshot;
BufferedImagecreateTableScreenshot(Component component)
create Table Screenshot
Component source;
Dimension sourceSize;
JTable table;
if (component instanceof JTable) {
    table = (JTable) component;
    if ((table.getTableHeader() == null) || !table.getTableHeader().isVisible()) {
        return createGeneralComponentScreenshot(component);
    source = table;
    sourceSize = table.getSize();
} else if (component instanceof JViewport && ((JViewport) component).getView() instanceof JTable) {
    JViewport viewport = (JViewport) component;
    table = (JTable) viewport.getView();
    if ((table.getTableHeader() == null) || !table.getTableHeader().isVisible()) {
        return createGeneralComponentScreenshot(component);
    if (table.getSize().height > viewport.getSize().height) {
        source = viewport;
        sourceSize = viewport.getSize();
    } else {
        source = table;
        sourceSize = table.getSize();
} else {
    throw new IllegalArgumentException("Component can only be JTable or JViewport holding JTable"); 
final JTableHeader tableHeader = table.getTableHeader();
Dimension tableHeaderSize = tableHeader.getSize();
BufferedImage tableScreenshot = new BufferedImage(sourceSize.width,
        tableHeaderSize.height + sourceSize.height, BufferedImage.TYPE_INT_RGB);
final Graphics tableScreenshotGraphics = tableScreenshot.getGraphics();
if (SwingUtilities.isEventDispatchThread()) {
    tableHeader.printAll(tableScreenshotGraphics);
} else {
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                tableHeader.printAll(tableScreenshotGraphics);
        });
    } catch (Exception e) {
tableScreenshotGraphics.translate(0, tableHeaderSize.height);
final Component printSrc = source;
if (SwingUtilities.isEventDispatchThread()) {
    printSrc.printAll(tableScreenshotGraphics);
} else {
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                printSrc.printAll(tableScreenshotGraphics);
        });
    } catch (Exception e) {
return tableScreenshot;
BufferedImagecreateTableScreenshot(Component component)
create Table Screenshot
Component source;
Dimension sourceSize;
JTable table;
if (component instanceof JTable) {
    table = (JTable) component;
    if ((table.getTableHeader() == null) || !table.getTableHeader().isVisible()) {
        return createGeneralComponentScreenshot(component);
    source = table;
    sourceSize = table.getSize();
} else if (component instanceof JViewport && ((JViewport) component).getView() instanceof JTable) {
    JViewport viewport = (JViewport) component;
    table = (JTable) viewport.getView();
    if ((table.getTableHeader() == null) || !table.getTableHeader().isVisible()) {
        return createGeneralComponentScreenshot(component);
    if (table.getSize().height > viewport.getSize().height) {
        source = viewport;
        sourceSize = viewport.getSize();
    } else {
        source = table;
        sourceSize = table.getSize();
} else {
    throw new IllegalArgumentException("Component can only be JTable or JViewport holding JTable"); 
final JTableHeader tableHeader = table.getTableHeader();
Dimension tableHeaderSize = tableHeader.getSize();
BufferedImage tableScreenshot = new BufferedImage(sourceSize.width,
        tableHeaderSize.height + sourceSize.height, BufferedImage.TYPE_INT_RGB);
final Graphics tableScreenshotGraphics = tableScreenshot.getGraphics();
if (SwingUtilities.isEventDispatchThread()) {
    tableHeader.printAll(tableScreenshotGraphics);
} else {
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                tableHeader.printAll(tableScreenshotGraphics);
        });
    } catch (Exception e) {
tableScreenshotGraphics.translate(0, tableHeaderSize.height);
final Component printSrc = source;
if (SwingUtilities.isEventDispatchThread()) {
    printSrc.printAll(tableScreenshotGraphics);
} else {
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                printSrc.printAll(tableScreenshotGraphics);
        });
    } catch (Exception e) {
return tableScreenshot;