mercredi 3 juin 2015

How to influence the order of methodes in reflection

I want to create a Swingtable with a dynamic Layout, regarding the class data that is set as sources.
To be more specific:
I have a class with multiple attributes. When creating the table, I want to do it, so that the table looks: "which public getFunction with returntyp String are available" and use the attributes behind this functions as Columnnames and later also as source for the rows.

That is working at the moment.

My problem now is:
How can I ensure a specific order of my Columns with this approach? for example i have a column "ID","callsign","categorie".
I want to display them in this order.
No metter how i order the methodes in sourceCode, the columns are allways in the same order ("ID","categorie","callsign").

        java.lang.reflect.Method methodes[] = null;
        methodes = classObjectOfT.getMethods();
        List<String> tempList=new ArrayList<String>();
        for (java.lang.reflect.Method m: methodes)
        {
            if (m.getReturnType().equals(String.class)&&m.getName().startsWith("get"))
            {
                tempList.add(m.getName().substring(3));
            }
        }
        columnNames=(String[]) tempList.toArray(new String[tempList.size()]);

above is the code i use for retriving the columnames.

A workaround would be to name the attributes/getMethodes "ID_00","Callsign_01","Categorie_02" and do the ordering by using the last 2 chars of the String, but that would be rather ugly and i'm searching for a cleaner solution.





Aucun commentaire:

Enregistrer un commentaire