private static void drawSelection(JTable table, int column, Graphics g, final int width) { int y = 0; final int[] rows = table.getSelectedRows(); final int height = table.getRowHeight(); for (int row : rows) { final TableCellRenderer renderer = table.getCellRenderer(row, column); final Component component = renderer.getTableCellRendererComponent( table, table.getValueAt(row, column), false, false, row, column); g.translate(0, y); component.setBounds(0, 0, width, height); boolean wasOpaque = false; if (component instanceof JComponent) { final JComponent j = (JComponent) component; if (j.isOpaque()) wasOpaque = true; j.setOpaque(false); } component.paint(g); if (wasOpaque) { ((JComponent) component).setOpaque(true); } y += height; g.translate(0, -y); } }
private String extractContent( DataGridPanel grid, String delimiter, boolean saveHeader, boolean ecloseWithDowbleQuotes) { JTable _table = grid.getTable(); int numcols = _table.getSelectedColumnCount(); int numrows = _table.getSelectedRowCount(); if (numcols > 0 && numrows > 0) { StringBuffer sbf = new StringBuffer(); int[] rowsselected = _table.getSelectedRows(); int[] colsselected = _table.getSelectedColumns(); if (saveHeader) { // put header name list for (int j = 0; j < numcols; j++) { String text = (String) _table .getTableHeader() .getColumnModel() .getColumn(colsselected[j]) .getHeaderValue(); sbf.append(text); if (j < numcols - 1) sbf.append(delimiter); } sbf.append("\n"); } // put content for (int i = 0; i < numrows; i++) { for (int j = 0; j < numcols; j++) { Object value = _table.getValueAt(rowsselected[i], colsselected[j]); String _value = ""; if (value != null) { _value = value.toString(); } if (ecloseWithDowbleQuotes) { sbf.append("\"").append(_value).append("\""); } else { sbf.append(_value); } if (j < numcols - 1) sbf.append(delimiter); } sbf.append("\n"); } // StringSelection stsel = new StringSelection(sbf.toString()); // Clipboard system = Toolkit.getDefaultToolkit().getSystemClipboard(); // system.setContents(stsel, stsel); return sbf.toString(); } return null; }