下面列出了javax.swing.JTable#getSelectedColumnCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public JMenu getMenu(final JTable jTable, final List<E> items) {
String text = null;
EnumTableColumn<?> column = null;
boolean isNumeric = false;
boolean isDate = false;
int columnIndex = jTable.getSelectedColumn();
if (jTable.getSelectedColumnCount() == 1 //Single cell (column)
&& jTable.getSelectedRowCount() == 1 //Single cell (row)
&& items.size() == 1 //Single element
&& !(items.get(0) instanceof SeparatorList.Separator) //Not Separator
&& columnIndex >= 0 //Shown column
&& columnIndex < getShownColumns().size()) { //Shown column
column = getShownColumns().get(columnIndex);
isNumeric = isNumeric(column);
isDate = isDate(column);
text = FilterMatcher.format(getColumnValue(items.get(0), column.name()), false);
}
return new FilterMenu<E>(gui, column, text, isNumeric, isDate);
}
/**
* Reads the cell values of selected cells of the <code>tmodel</code> and
* uploads into clipboard in supported format
*
* @param isCut CUT flag,<code>true</code> for CUT and <code>false</code>
* for COPY
* @param table the source for the action
* @see #escape(java.lang.Object)
*/
private static void copyToClipboard(boolean isCut, JTable table) {
try {
int numCols = table.getSelectedColumnCount();
int numRows = table.getSelectedRowCount();
int[] rowsSelected = table.getSelectedRows();
int[] colsSelected = table.getSelectedColumns();
if (numRows != rowsSelected[rowsSelected.length - 1] - rowsSelected[0] + 1 || numRows != rowsSelected.length
|| numCols != colsSelected[colsSelected.length - 1] - colsSelected[0] + 1 || numCols != colsSelected.length) {
JOptionPane.showMessageDialog(null, "Invalid Selection", "Invalid Selection", JOptionPane.ERROR_MESSAGE);
return;
}
StringBuilder excelStr = new StringBuilder();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
excelStr.append(escape(table.getValueAt(rowsSelected[i], colsSelected[j])));
if (isCut) {
if (table.isCellEditable(rowsSelected[i], colsSelected[j])) {
table.setValueAt("", rowsSelected[i], colsSelected[j]);
}
}
if (j < numCols - 1) {
excelStr.append(CELL_BREAK);
}
}
if (i < numRows - 1) {
excelStr.append(LINE_BREAK);
}
}
if (!excelStr.toString().isEmpty()) {
StringSelection sel = new StringSelection(excelStr.toString());
CLIPBOARD.setContents(sel, sel);
}
} catch (HeadlessException ex) {
Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void copyToClipboard(JTable table, boolean isCut) {
int numCols = table.getSelectedColumnCount();
int numRows = table.getSelectedRowCount();
int[] rowsSelected = table.getSelectedRows();
int[] colsSelected = table.getSelectedColumns();
if (numRows != rowsSelected[rowsSelected.length - 1] - rowsSelected[0] + 1 || numRows != rowsSelected.length
|| numCols != colsSelected[colsSelected.length - 1] - colsSelected[0] + 1 || numCols != colsSelected.length) {
Logger.getLogger(XTableUtils.class.getName()).info("Invalid Copy Selection");
return;
}
if (table.getModel() instanceof UndoRedoModel) {
((UndoRedoModel) table.getModel()).startGroupEdit();
}
StringBuilder excelStr = new StringBuilder();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
excelStr.append(escape(table.getValueAt(rowsSelected[i], colsSelected[j])));
if (isCut) {
table.setValueAt("", rowsSelected[i], colsSelected[j]);
}
if (j < numCols - 1) {
excelStr.append(CELL_BREAK);
}
}
excelStr.append(LINE_BREAK);
}
if (table.getModel() instanceof UndoRedoModel) {
((UndoRedoModel) table.getModel()).stopGroupEdit();
}
StringSelection sel = new StringSelection(excelStr.toString());
CLIPBOARD.setContents(sel, sel);
}
/**
* Uses the current table selection to update the cell range selection.
*/
void updateCellRangeByTableSelection(JTable contentTable) {
int columnIndexStart = contentTable.getSelectedColumn();
int rowIndexStart = contentTable.getSelectedRow();
int columnIndexEnd = columnIndexStart + contentTable.getSelectedColumnCount() - 1;
int rowIndexEnd = rowIndexStart + contentTable.getSelectedRowCount() - 1;
setCellRangeSelection(new CellRangeSelection(columnIndexStart, rowIndexStart, columnIndexEnd, rowIndexEnd));
}