So, I was looking for a way to sort jFileChooser by date and I found this discussion: Start a JFileChooser with files ordered by date
The solution works. Though, this code perplexes me:
JTable table = SwingUtils.getDescendantsOfType(JTable.class, fileChooser).get(0);
I decided to use println() to check the type of table instance and this is the result: sun.swing.FilePane$6...
This FilePane piqued my interest. So, I decided to look for the source code of FilePane and I found this: http://www.docjar.com/html/api/sun/swing/FilePane.java.html
In the source code, FilePane doesn't extend JTable. Though, FilePane has JTable as component. How is it possible for FilePane to be casted to JTable?
I looked at the SwingUtils source and found some implementations of getDescendantsOfType() method.
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;
}
I've tried to cast FilePane to JTable using cast operator and it didn't work. I have little knowledge about Class<T> class so, I'm not very familiar with the methods of this class.