我所做的是延长
BaseTableHeaderUI
. 我如何发现扩展它是我需要做的,这是最困难的部分:
-
增加了一个
RowSorterListener
到
JTable.getRowSorter
-
打印调用堆栈的时间
sorterChanged
被称为
-
调用堆栈将责任推到
javax.swing.plaf.basic.BasicTableHeaderUI
用于排序事件
-
找到的源代码
BasicTableHeaderUI
-
排序仅由执行
BasicTableHeaderUI.MouseInputHandler
-
扩展的
basicTableHeaderUI.mouseInputHandler
仅当没有修饰符时才调用它的super
-
摆好桌子
JTableHeaderUI
到新班级
这是修改过的
SorterChooser.java
:
import javax.swing.*;
public class SorterChooser {
private static void createAndShowGUI() {
JFrame frame = new JFrame("SorterChooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object[][] data = { {"a", "1"}, {"b", "3"}, {"c", "2"}, {"d", "4"} };
String[] colnames = { "c1", "c2" };
JTable tab = new JTable(data, colnames);
tab.getTableHeader().setUI(new SorterChooserTableHeaderUI()); // new code
JScrollPane sp = new JScrollPane(tab);
tab.setAutoCreateRowSorter(true);
new TableColumnManager(tab, true);
frame.getContentPane().add(sp);
frame.pack();
frame.setVisible(true);
}
public static void main (String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
这是扩展的
BasicTableHeaderUI.java
:
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.BasicTableHeaderUI;
public class SorterChooserTableHeaderUI extends BasicTableHeaderUI {
public class MouseInputHandler extends BasicTableHeaderUI.MouseInputHandler {
public void mouseClicked (MouseEvent ev) {
if (ev.getModifiersEx() == 0)
super.mouseClicked(ev);
}
}
@Override
protected MouseInputListener createMouseInputListener () {
return new MouseInputHandler();
}
}