我有一个属性 AutoCreateRowSorter 设置为 true 的 JTable。单击时效果很好,但是如果我在单击期间将鼠标移动 1px,则不会出现排序,这在某些情况下会让人不舒服。如何解决这个问题?
UPD 我只想在鼠标释放时出现排序。
一个肮脏的(阅读:如果你不是绝对绝望并且确切地知道你在做什么,就不要这样做!)方法是挂钩到由 uiDelegate 安装的 MouseListener 并将排序触发器移动到释放的方法中。这涉及
自定义监听器:
public static class EventHook implements MouseListener {
private JTableHeader header;
private MouseListener delegate;
public EventHook(JTableHeader header) {
this.header = header;
installHook();
}
protected void installHook() {
MouseListener[] listeners = header.getMouseListeners();
for (int i = 0; i < listeners.length; i++) {
MouseListener l = listeners[i];
if (l.getClass().getName().contains("TableHeaderUI")) {
this.delegate = l;
listeners[i] = this;
}
header.removeMouseListener(l);
}
for (MouseListener l : listeners) {
header.addMouseListener(l);
}
}
public void uninstallHook() {
MouseListener[] listeners = header.getMouseListeners();
for (int i = 0; i < listeners.length; i++) {
MouseListener l = listeners[i];
if (l == this) {
listeners[i] = delegate;
}
header.removeMouseListener(l);
}
for (MouseListener l : listeners) {
header.addMouseListener(l);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// sort before calling super
sort(e);
delegate.mouseReleased(e);
}
private void sort(MouseEvent e) {
if (!header.isEnabled()) {
return;
}
// do nothing if dragged
if (header.getDraggedDistance() != 0) {
return;
}
if (e.getClickCount() % 2 == 1 &&
SwingUtilities.isLeftMouseButton(e)) {
JTable table = header.getTable();
RowSorter sorter;
if (table != null && (sorter = table.getRowSorter()) != null) {
int columnIndex = header.columnAtPoint(e.getPoint());
if (columnIndex != -1) {
columnIndex = table.convertColumnIndexToModel(
columnIndex);
sorter.toggleSortOrder(columnIndex);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// swallow the clicked - want to trigger sort on released
}
@Override
public void mousePressed(MouseEvent e) {
delegate.mousePressed(e);
}
@Override
public void mouseEntered(MouseEvent e) {
delegate.mouseEntered(e);
}
@Override
public void mouseExited(MouseEvent e) {
delegate.mouseExited(e);
}
}
子类化的 JTableHeader
public static class MyTableHeader extends JTableHeader {
private EventHook hook;
public MyTableHeader(TableColumnModel model) {
super(model);
}
@Override
public void updateUI() {
if (hook != null) {
hook.uninstallHook();
hook = null;
}
super.updateUI();
hook = new EventHook(this);
}
}
用法,子类 JTable 并覆盖 createDefaultTableHeader 或手动设置标题:
// either subclass
JTable table = new JTable(new AncientSwingTeam()) {
@Override
protected JTableHeader createDefaultTableHeader() {
return new MyTableHeader(getColumnModel());
}
};
table.setAutoCreateRowSorter(true);