Return to Snippet

Revision: 69554
at July 16, 2015 11:35 by lasagna7355608


Initial Code
//Main
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;

public class Main extends Application{

    public ArrayList<Method> methodArrayList = new ArrayList<Method>(Arrays.asList(Math.class.getMethods()));
    public static ArrayList<Method> validMethodList = new ArrayList<Method>();
    public ArrayList<String> methodNameList = new ArrayList<String>();

    public static void main(String[] args) {
        launch(args);
    }


    public Scene createScene(){
        VBox root = new VBox();
        root.setSpacing(10);
        root.setPadding(new Insets(10,10,10,10));

        Label x = new Label("X");

        TextField value = new TextField();
        value.setId("value");

        for(Method method : methodArrayList)
        {
                if (method.getParameterCount() == 1 && method.getReturnType().equals(Double.TYPE)){
                    Parameter parameter = method.getParameters()[0];
                    if(parameter.getType().equals(Double.TYPE)) {
                        methodNameList.add(method.getName());
                        validMethodList.add(method);
                    }
                }
        }

        ComboBox<String> methodComboBox = new ComboBox<String>(FXCollections.observableArrayList(methodNameList));
        methodComboBox.setId("method");

        Button btnCalc = new Button("Calculate");
        btnCalc.setId("button");

        Label lblAnswer = new Label("Answer: ?");
        lblAnswer.setId("answer");

        root.getChildren().addAll(x, value, methodComboBox, btnCalc, lblAnswer);

        Scene scene = new Scene(root);

        return scene;

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Reflection");
        Scene scene = createScene();
        primaryStage.setScene(scene);
        Controller controller = new Controller();
        controller.connectSceneUI(scene);
        primaryStage.show();
    }
}

//Controller
import javafx.scene.Scene;
import javafx.scene.control.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Controller {

    Button button = new Button();
    Label label = new Label();
    ComboBox comboBox = new ComboBox();
    TextField textField = new TextField();

    public void connectSceneUI(Scene scene) {
        button = (Button) scene.lookup("#button");
        label = (Label) scene.lookup("#answer");
        comboBox = (ComboBox) scene.lookup("#method");
        textField = (TextField) scene.lookup("#value");

        button.setOnAction((event) -> {
            ArrayList<Method> validMethodList = nmmu.wrap301.Main.validMethodList;
            Method method = validMethodList.get(comboBox.getSelectionModel().getSelectedIndex());
            try {
                Double answer = (Double) method.invoke(null,Double.parseDouble(textField.getText()));
                label.setText("Answer: " + answer.toString());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        });
    }
}

Initial URL
reflectionmathsexample

Initial Description
A nifty little example of reflection in Java using the Math class.

Initial Title
Example of reflection in Java using Math Class

Initial Tags


Initial Language
Java