I have implemented an application that simulates a shop in java with all information placed in a database made in SQL. My issue is with a reflection technique which I have to implement in order to show the list of elements from a table. I have tried this method(the first answer, the one with metaData) Most simple code to populate JTable from ResultSet However I have been told that this is not a reflection method. I made this, guided by the teacher. But, I have no idea how to actually call it in my function and where the "object " is supposed to come from. The first function returns the headers and the second extracts the info and creates the table.
public class ReflectionExample {
protected static final Logger LOGGER = Logger.getLogger(ClientDao.class.getName());
public static ArrayList retrieveProperties(Object object) {
ArrayList<String> ob = new ArrayList<String>();
Class obiect = object.getClass();
int i = 0;
for (Field field : object.getClass().getDeclaredFields()) {
field.setAccessible(true); // set modifier to public
ob.add(field.getName());
i++;
}
return ob;
}
public static JTable retrievePropertiesM(List<?> object, Object[]lit) {
Class obiect = object.getClass();
Object [][] matrice = new Object[object.size()][20];
//Object[] lit=null;
int row = 0;
for (Object o : object) {
int col = 0;
for (Field field : o.getClass().getDeclaredFields()) {
field.setAccessible(true); // set modifier to public
Object value;
try {
value = field.get(object);
matrice[row][col]=value;
//lit[col]=field.getName();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
col++;
}
row++;
}
// Object[] lit=retrieveProperties(object);
JTable t = new JTable(matrice,lit);
return t;
}
}
However, I am in doubt as to why the first way I made was not a reflection? As per all, I did not understood what reflection means beside that a method with reflection can be called by any type of object from the database. Some explanations or maybe somewhere I can understand better reflection?
Aucun commentaire:
Enregistrer un commentaire