Java JTable Cell getCurrentSelectionContent(JTable table, String lineBreak, String cellBreak)

Here you can find the source of getCurrentSelectionContent(JTable table, String lineBreak, String cellBreak)

Description

Gets the content of current JTable selection as String or returns null, if the selection is invalid.

License

LGPL

Parameter

Parameter Description
table a parameter
lineBreak a parameter
cellBreak a parameter

Declaration

public static String getCurrentSelectionContent(JTable table, String lineBreak, String cellBreak) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.logging.Logger;

import javax.swing.JTable;

public class Main {
    private static Logger logger = Logger.getLogger("uk.ac.starlink.splat.util.JTableUtilities");

    /**/*from  w  ww. ja v  a  2 s  .  c o m*/
     * Gets the content of current JTable selection as String
     * or returns null, if the selection is invalid.
     * 
     * @param table
     * @param lineBreak
     * @param cellBreak
     * @return
     */
    public static String getCurrentSelectionContent(JTable table, String lineBreak, String cellBreak) {
        int numCols = table.getSelectedColumnCount();
        int numRows = table.getSelectedRowCount();
        int[] rowsSelected = table.getSelectedRows();
        int[] colsSelected = table.getSelectedColumns();

        return getContent(table, lineBreak, cellBreak, numCols, numRows, rowsSelected, colsSelected);
    }

    private static String getContent(JTable table, String lineBreak, String cellBreak, int columnCount,
            int rowCount, int[] selectedRowsCount, int[] selectedColumsCount) {

        if (columnCount > 0 && rowCount > 0) {
            StringBuffer value = new StringBuffer();

            for (int i = 0; i < rowCount; i++) {
                for (int j = 0; j < columnCount; j++) {
                    value.append(escapeContentBreaks(table.getValueAt(selectedRowsCount[i], selectedColumsCount[j]),
                            lineBreak, cellBreak));
                    if (j < columnCount - 1) {
                        value.append(cellBreak);
                    }
                }
                value.append(lineBreak);
            }

            return value.toString();
        } else {
            logger.warning("Invalid selection.");
            return null;
        }
    }

    private static String escapeContentBreaks(Object cell, String lineBreak, String cellBreak) {
        return cell == null ? "" : cell.toString().replace(lineBreak, " ").replace(cellBreak, " ");
    }
}

Related

  1. defaultTableCellSize()
  2. getCellBounds(JTable table, int top, int bottom)
  3. getCellRectangle(JTable table, int row, int column)
  4. getColumnNames(JTable table, String lineBreak, String cellBreak)
  5. getContent(JTable table, String lineBreak, String cellBreak, int columnCount, int rowCount, int[] selectedRowsCount, int[] selectedColumsCount)
  6. getIntercellWidth(JTable table)
  7. getTableFocusCellForeground()
  8. getWidestCellInColumn(JTable table, TableColumn col)
  9. getWidestCellInColumn(TableColumn col, JTable table)