下面列出了javax.swing.JTable#setColumnSelectionInterval ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void highLightCell(JTable table, int row, int column) {
table.setRowSelectionInterval(row, row);
table.setColumnSelectionInterval(column, column);
scrollToSelectedRow(table);
}
public static void focusRowTable(JTable table, int row) {
table.setRowSelectionInterval(row, row);
table.setColumnSelectionInterval(0, table.getColumnCount() - 1);
scrollToSelectedRow(table);
}
public static void focusColumnTable(JTable table, int column) {
table.setColumnSelectionInterval(column, column);
table.setRowSelectionInterval(0, table.getColumnCount() - 1);
}
public static void focusCellTable(JTable table, int row, int column) {
table.setRowSelectionInterval(row, row);
table.setColumnSelectionInterval(column, column);
}
private void selectClickedCell(final MouseEvent e) {
Object source = e.getSource();
if (source instanceof JTable) {
JTable jSelectTable = (JTable) source;
Point point = e.getPoint();
if (!jSelectTable.getVisibleRect().contains(point)) { //Ignore clickes outside table
return;
}
int clickedRow = jSelectTable.rowAtPoint(point);
int clickedColumn = jSelectTable.columnAtPoint(point);
//Rows
boolean clickInRowsSelection;
if (jSelectTable.getRowSelectionAllowed()) { //clicked in selected rows?
clickInRowsSelection = false;
int[] selectedRows = jSelectTable.getSelectedRows();
for (int i = 0; i < selectedRows.length; i++) {
if (selectedRows[i] == clickedRow) {
clickInRowsSelection = true;
break;
}
}
} else { //Row selection not allowed - all rows selected
clickInRowsSelection = true;
}
//Column
boolean clickInColumnsSelection;
if (jSelectTable.getColumnSelectionAllowed()) { //clicked in selected columns?
clickInColumnsSelection = false;
int[] selectedColumns = jSelectTable.getSelectedColumns();
for (int i = 0; i < selectedColumns.length; i++) {
if (selectedColumns[i] == clickedColumn) {
clickInColumnsSelection = true;
break;
}
}
} else { //Column selection not allowed - all columns selected
clickInColumnsSelection = true;
}
//Clicked outside selection, select clicked cell
if ( (!clickInRowsSelection || !clickInColumnsSelection) && clickedRow >= 0 && clickedColumn >= 0) {
jSelectTable.setRowSelectionInterval(clickedRow, clickedRow);
jSelectTable.setColumnSelectionInterval(clickedColumn, clickedColumn);
}
}
}