jeudi 21 juillet 2016

Can I use reflection to dynamically bind methods to my JTextFields?

I have a page with several JTextFields. All of their variable names start with txt i.e. txtCarArea, txtRopeLength,....

I have an orderObject, that has several fields and setters / getters to use them.

The textfields get filled with the values from the fields of the orderObject like this:

// Around 50 fields get filled like this
txtCarArea.setText(orderObject.getCar_area());
....

The user then can change the values.

Now I could get the values of every textfield and put it into the orderObject after the user clicks on a button like "Apply changes", but this again would mean I have to write 50 setter-uses and fire all of them, even if the user only changed a value in one field:

@Override
public void actionPerformed(ActionEvent e) {
    // Again: Around 50 uses of the different setters
    orderObject.setCar_area(txtCarArea.getText());
    orderObject.setRope_length(txtRopeLength.getText());
    orderObject.setDoughID(txtDoughID.getText());
    (...)
}

This feels very bloated? I have to write and maintain those 50 calls.

Therefore I've read about reflection and tried to just use the setter-methods of the textfields that are changed and not all of them.

@Override
        public void actionPerformed(ActionEvent e) {
            orderObject.setCar_area(txtCarArea.getText());
            Method[] methods = orderObject.getClass().getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                String methodName = methods[i].getName();
                if (methodName.startsWith("set")) {
                    methodName = "txt" + methodName.substring(3);
                    String newMethodName = methodName.replace("_", "");
                    Method method = null;
                    try {
                        // "setCarArea" is hardcoded by me
                        method = orderObject.getClass().getMethod("setCarArea", String.class);
                    } catch (NoSuchMethodException e1) {
                        e1.printStackTrace();
                    } catch (SecurityException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        // How to invoke dynamically for every textfield?
                        method.invoke(orderObject, "1070");
                    } catch (IllegalAccessException e1) {
                        e1.printStackTrace();
                    } catch (IllegalArgumentException e1) {
                        e1.printStackTrace();
                    } catch (InvocationTargetException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }

But since I dont know to find the pair of orderObject-setter + JTextField where the value is, I am stuck now.

So how can I dynamically get the correct setter-methods in the orderObject to input the values from the JTextFields?





Aucun commentaire:

Enregistrer un commentaire