I'm pretty new to java and I have a small issue with my code. The code works pretty well but I feel like it can be improved.
Here's the code:
if(evt.getStateChange()==1){
String value = "-MyAppArgument ";
String temp = CustomARG.getText();
CustomARG.setText(temp+""+value);
}else if(evt.getStateChange()!= 1){
String value = "-MyAppArgument ";
CustDEVARGSargsTextField.setText(CustDEVARGSargsTextField.getText().replace(value,""));
}
I have 4 check box buttons, each has an argument like the one above. My idea of optimizing was to construct a string based on selected buttons and use the string in actual processbuilder, but I'm not sure how can I do that. The question is how can I optimize this, is my idea good?
Another option is, rather than add / remove parameters in response to a checked event, instead completely rebuild the string from scratch in response to each event. Not necessarily "optimized" but you don't really need this to be optimized, you just need it to function and to be maintainable.
For example, pseudo-code (feel free to use a StringBuilder
, doesn't really matter):
void updateParameterString () {
String parameterString = ""; // constant params can be initialized here too
if (checkbox1 is checked) parameterString += " -arg1";
if (checkbox2 is checked) parameterString += " -arg2";
if (checkbox3 is checked) parameterString += " -arg3";
if (checkbox4 is checked) parameterString += " -arg4";
CustDEVARGSargsTextField.setText(parameterString); // option: trim the string
}
void onAnyCheckButtonStateChange () {
updateParameterString();
}
Another advantage of this approach is you can e.g. initialize the parameter string text box using the initial default values of the checkboxes with the same code, by, say, setting check states when you create the checkboxes then calling updateParameterString()
when the form is displayed.
Other permutations of the above are of course possible, depending on what is most appropriate, e.g. replacing updateParameterString()
with a method that just generates and returns the string and using that in the correct places instead.