📄 anyselectiontableui.java
字号:
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellEditor;
import javax.swing.plaf.basic.BasicTableUI;
import javax.swing.plaf.ComponentUI;
import javax.swing.event.MouseInputListener;
/**
* This class doesn't change the L&F of the JTable but
* listens to mouseclicks and updates the TableSelectionModel.
* @author Jan-Friedrich Mutter (jmutter@bigfoot.de)
*/
public class AnySelectionTableUI extends BasicTableUI {
public static ComponentUI createUI(JComponent c) {
return new AnySelectionTableUI();
}
protected MouseInputListener createMouseInputListener() {
return new AnySelectionMouseInputHandler();
}
/**
* to get access to the table from the inner class MyMouseInputHandler
*/
protected JTable getTable() {
return table;
}
/**
* updates the TableSelectionModel.
*/
protected void updateTableSelectionModel(int row, int column,
boolean ctrlDown, boolean shiftDown) {
AnySelectionTable t = (AnySelectionTable)getTable();
column = t.convertColumnIndexToModel(column);
TableSelectionModel tsm = t.getTableSelectionModel();
int anchorIndex = tsm.getListSelectionModelAt(column).getAnchorSelectionIndex();
if (ctrlDown) {
if (tsm.isSelected(row, column)) {
tsm.removeSelection(row, column);
} else {
tsm.addSelection(row, column);
}
} else if ((shiftDown) && (anchorIndex != -1)){
tsm.setSelectionInterval(anchorIndex, row, column);
} else {
tsm.setSelection(row, column);
}
} //updateTableSelectionModel()
/**
* Almost the same implementation as its super class.
* Except updating the TableSelectionModel rather than the
* default ListSelectionModel.
*/
//Some methods which are called in the super class are private.
//Thus I couldn't call them. Calling the method of the super
//class itself should do it, but you never know. Sideeffects may occur...
public class AnySelectionMouseInputHandler extends MouseInputHandler {
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
if (!SwingUtilities.isLeftMouseButton(e)) {
return;
}
/*if (phantomMousePressed == true) {
return;
}
phantomMousePressed = true;*/
Point p = e.getPoint();
int row = getTable().rowAtPoint(p);
int column = getTable().columnAtPoint(p);
// The autoscroller can generate drag events outside the Table's range.
if ((column == -1) || (row == -1)) {
return;
}
/* Adjust the selection if the event was not forwarded
* to the editor above *or* the editor declares that it
* should change selection even when events are forwarded
* to it.
*/
// PENDING(philip): Ought to convert mouse event, e, here.
//if (!repostEvent || table.getCellEditor().shouldSelectCell(e)) {
TableCellEditor tce = getTable().getCellEditor();
if ((tce==null) || (tce.shouldSelectCell(e))) {
getTable().requestFocus();
//setValueIsAdjusting(true);
//if (getTable().getValueAt(row, column) != null) {
updateTableSelectionModel(row, column, e.isControlDown(), e.isShiftDown());
//need to do this because JTable is not painted properly when
//user does Shift-Click on a column where a not selected
//cell is the anchor ...
if (e.isShiftDown())
getTable().repaint();
//}
}
}//mousePressed()
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -