What is the best way to handle JComponents based on Java Data Types?

40 Views Asked by At

I have a program which creates a gui to trigger APIs, and using Reflect im reading the necessary parameters of the API and want to create a user input so that the user can prepare the API before execution.

For example:

void getAccountInfo(String name, int id, long customer_id, double balnce, boolean isActive)

What I'm trying to figure out is what is the best way to dynamically display the JComponent inputs based on the datatypes of the API parameters.

I created the following switch statment which is basically reading the datatype of each variable and creates a different JComponent, but I found out that this will cause some issues:

switch(entry.getValue().getSimpleName()){
    case "int":
        //SpinnerModel intSpinnerModel = new SpinnerNumberModel(0, Integer.MIN_VALUE, Integer.MAX_VALUE, 1);
        inputComponent = new JSpinner();
        break;
    case "long":
        //SpinnerModel longSpinnerModel = new SpinnerNumberModel(0, Long.MIN_VALUE, Long.MAX_VALUE, 1);
        inputComponent = new JSpinner();
        break;
    case "double":
        //SpinnerModel doubleSpinnerModel = new SpinnerNumberModel(0.00, -1.0e100, 1.0e100, 0.01);
        inputComponent = new JSpinner();
        break;
    case "boolean":
        inputComponent = new JComboBox<String>(new String[] { null, "true", "false" });
        break;
    case "String":
        inputComponent = new JTextField();
        break;
}

For now this works great but it will cause issues in the future:

  1. In case of char I cannot add a JTextField because the user might pass more than 1 character
  2. In case of numeric values I cannot use a JSpinner because if int then I should not allow decimals, if float or double I should not allow more than 2 decimals. I tried using SpinnerModel but seems like is not working properly.

Is there an alternative approach to what I'm trying to achieve here or should I just focus on fixing the existing code! For this I'm only using java.swing and java.awt


Based on Comments by Abra enter image description here

0

There are 0 best solutions below