Bonjour,

Je veux utiliser les nouvelles fonctions de Java 6 telles que le TableRowSorter dans mon application tout en la laissant compatible avec Java 5. J'ai essayé avec le package reflection mais sans succès. Qu'est-ce qui ne va pas dans mon code ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  public static void addRowSorter(JTable table, TableModel model) {
    //table.setRowSorter(new TableRowSorter<TableModel>(model));
    try {
      Class tableRowSorterClass = Class.forName("javax.swing.table.TableRowSorter");
      Constructor ctor = tableRowSorterClass.getConstructor(new Class[] { TableModel.class });
      Object rowSorter = ctor.newInstance(model);
      Method method = table.getClass().getMethod("setRowSorter", new Class[] { tableRowSorterClass });
      method.invoke(table, new Object[] { rowSorter });
    } catch (ClassNotFoundException e) {
      log.debug("ClassNotFoundException: " + e.getMessage());
      // Nothing to be done, JVM < 6
    } catch (NoSuchMethodException e) {
      log.error("NoSuchMethodException: " + e.getMessage());
    } catch (InvocationTargetException e) {
      log.error("InvocationTargetException: " + e.getMessage());
    } catch (IllegalAccessException e) {
      log.error("IllegalAccessException: " + e.getMessage());
    } catch (ClassCastException e) {
      log.error("ClassCastException: " + e.getMessage());
    } catch (Throwable e) {
      log.error("Throwable: " + e.getClass().getName() + " - " + e.getMessage());
    }
  }
Résultat: NoSuchMethodException: javax.swing.JTable.setRowSorter(javax.swing.table.TableRowSorter)