为什么 sun.swing.FilePane 可以转换为 javax.swing.table?
Why sun.swing.FilePane can be casted to javax.swing.table?
所以,我正在寻找一种按日期对 jFileChooser 进行排序的方法,我发现了这个讨论:Start a JFileChooser with files ordered by date
解决方案有效。虽然,这段代码让我感到困惑:
JTable table = SwingUtils.getDescendantsOfType(JTable.class, fileChooser).get(0);
我决定使用 println() 来检查 table 实例的类型,结果如下:sun.swing.FilePane...
这个 FilePane 引起了我的兴趣。所以,我决定寻找 FilePane 的源代码,我发现了这个:http://www.docjar.com/html/api/sun/swing/FilePane.java.html
在源代码中,FilePane 没有扩展JTable。虽然,FilePane 有 JTable 作为组件。如何将 FilePane 转换为 JTable?
我查看了 SwingUtils 源代码并找到了 getDescendantsOfType() 方法的一些实现。
public static <T extends JComponent> List<T> getDescendantsOfType(
Class<T> clazz, Container container) {
return getDescendantsOfType(clazz, container, true);
}
public static <T extends JComponent> List<T> getDescendantsOfType(
Class<T> clazz, Container container, boolean nested) {
List<T> tList = new ArrayList<T>();
for (Component component : container.getComponents()) {
if (clazz.isAssignableFrom(component.getClass())) {
tList.add(clazz.cast(component));
}
if (nested || !clazz.isAssignableFrom(component.getClass())) {
tList.addAll(SwingUtils.<T>getDescendantsOfType(clazz,
(Container) component, nested));
}
}
return tList;
}
我尝试使用转换运算符将 FilePane 转换为 JTable,但没有成功。我对Class<T>class了解甚少,所以对这个class.
的方法不是很熟悉
In the source code, FilePane doesn't extend JTable.
正确。
How is it possible for FilePane to be casted to JTable?
不能。
sun.swing.FilePane...
$6 暗示它是在 FilePane class 中定义的内部 class。
内部 class 名称从 $1 开始,每个内部 class 增加。
从class可以看出:
final JTable detailsTable = new JTable(getDetailsTableModel()) {
// Handle Escape key events here
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE && getCellEditor() == null) {
// We are not editing, forward to filechooser.
chooser.dispatchEvent(e);
return true;
}
return super.processKeyBinding(ks, e, condition, pressed);
}
public void tableChanged(TableModelEvent e) {
super.tableChanged(e);
if (e.getFirstRow() == TableModelEvent.HEADER_ROW) {
// update header with possibly changed column set
updateDetailsColumnModel(this);
}
}
};
您可以看到 JTable 内部 class 覆盖了:
- processKeyBinding(),以及
- tableChanged()
方法。
当您编译源代码并使用内部 classes 时,您应该能够看到内部 class 文件。
所以,我正在寻找一种按日期对 jFileChooser 进行排序的方法,我发现了这个讨论:Start a JFileChooser with files ordered by date
解决方案有效。虽然,这段代码让我感到困惑:
JTable table = SwingUtils.getDescendantsOfType(JTable.class, fileChooser).get(0);
我决定使用 println() 来检查 table 实例的类型,结果如下:sun.swing.FilePane...
这个 FilePane 引起了我的兴趣。所以,我决定寻找 FilePane 的源代码,我发现了这个:http://www.docjar.com/html/api/sun/swing/FilePane.java.html
在源代码中,FilePane 没有扩展JTable。虽然,FilePane 有 JTable 作为组件。如何将 FilePane 转换为 JTable?
我查看了 SwingUtils 源代码并找到了 getDescendantsOfType() 方法的一些实现。
public static <T extends JComponent> List<T> getDescendantsOfType(
Class<T> clazz, Container container) {
return getDescendantsOfType(clazz, container, true);
}
public static <T extends JComponent> List<T> getDescendantsOfType(
Class<T> clazz, Container container, boolean nested) {
List<T> tList = new ArrayList<T>();
for (Component component : container.getComponents()) {
if (clazz.isAssignableFrom(component.getClass())) {
tList.add(clazz.cast(component));
}
if (nested || !clazz.isAssignableFrom(component.getClass())) {
tList.addAll(SwingUtils.<T>getDescendantsOfType(clazz,
(Container) component, nested));
}
}
return tList;
}
我尝试使用转换运算符将 FilePane 转换为 JTable,但没有成功。我对Class<T>class了解甚少,所以对这个class.
的方法不是很熟悉In the source code, FilePane doesn't extend JTable.
正确。
How is it possible for FilePane to be casted to JTable?
不能。
sun.swing.FilePane...
$6 暗示它是在 FilePane class 中定义的内部 class。
内部 class 名称从 $1 开始,每个内部 class 增加。
从class可以看出:
final JTable detailsTable = new JTable(getDetailsTableModel()) {
// Handle Escape key events here
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE && getCellEditor() == null) {
// We are not editing, forward to filechooser.
chooser.dispatchEvent(e);
return true;
}
return super.processKeyBinding(ks, e, condition, pressed);
}
public void tableChanged(TableModelEvent e) {
super.tableChanged(e);
if (e.getFirstRow() == TableModelEvent.HEADER_ROW) {
// update header with possibly changed column set
updateDetailsColumnModel(this);
}
}
};
您可以看到 JTable 内部 class 覆盖了:
- processKeyBinding(),以及
- tableChanged()
方法。
当您编译源代码并使用内部 classes 时,您应该能够看到内部 class 文件。