Here you can find the source of getCurrentSelectionContent(JTable table, String lineBreak, String cellBreak)
Parameter | Description |
---|---|
table | a parameter |
lineBreak | a parameter |
cellBreak | a parameter |
public static String getCurrentSelectionContent(JTable table, String lineBreak, String cellBreak)
//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, " "); } }