1

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.

Blue
  • 23
  • 5

1 Answers1

3

In the source code, FilePane doesn't extend JTable.

Correct.

How is it possible for FilePane to be casted to JTable?

It can't.

sun.swing.FilePane$6...

The $6 implies that it is an inner class defined in the FilePane class.

Inner class names start at $1 and increase for each inner class.

From the class you can see:

 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);
           }
       }
   };

You can see the JTable inner class overrides the:

  1. processKeyBinding(), and
  2. tableChanged()

methods.

You should be able to see inner class files when you compile your source code and use inner classes.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for pointing that inner class out! I was skimming the source code of FilePane without paying attention to inner classes. – Blue Aug 01 '21 at 23:12