Java: Is there a more efficient way to write JFrame elements into an array?

This is my first time posting so I apologize if the question is too vague or I do not describe my problem so well.

I am working on a Java JFrame project and have run into a small issue. I need to instantiate arrays of various frame elements. What I have done as of now works, however, it is very inefficient and I was wondering if there is a better, easier way to accomplish this task. The generated code generated by NetBeans instantiates my object as per there variable name that I have supplied. I am then writing a method where I am manually entering the array and which element that is equal to. Here is the auto-generated code:

private javax.swing.JTextField activityStartTxt0;
private javax.swing.JTextField activityStartTxt1;
private javax.swing.JTextField activityStartTxt2;
private javax.swing.JTextField activityStartTxt3;
private javax.swing.JTextField activityStartTxt4;

The same code is used to instantiate five activityEndTxt, 5 dayDropBox, 5 timeStartDropBox and 5 timeEndDropBox instances.

I then write these elements into arrays:

public void addArrays(){
        dayDropBox[0] = dayDropBox0;
        dayDropBox[1] = dayDropBox1;
        dayDropBox[2] = dayDropBox2;
        dayDropBox[3] = dayDropBox3;
        dayDropBox[4] = dayDropBox4;
        
        activityStartTxt[0] = activityStartTxt0;
        activityStartTxt[1] = activityStartTxt1;
        activityStartTxt[2] = activityStartTxt2;
        activityStartTxt[3] = activityStartTxt3;
        activityStartTxt[4] = activityStartTxt4;
        
        timeStartDropBox[0] = timeStartDropBox0;
        timeStartDropBox[1] = timeStartDropBox1;
        timeStartDropBox[2] = timeStartDropBox2;
        timeStartDropBox[3] = timeStartDropBox3;
        timeStartDropBox[4] = timeStartDropBox4;
        
        activityEndTxt[0] = activityEndsTxt0;
        activityEndTxt[1] = activityEndsTxt1;
        activityEndTxt[2] = activityEndsTxt2;
        activityEndTxt[3] = activityEndsTxt3;
        activityEndTxt[4] = activityEndsTxt4;
        
        timeEndDropBox[0] = timeEndDropBox0;
        timeEndDropBox[1] = timeEndDropBox1;
        timeEndDropBox[2] = timeEndDropBox2;
        timeEndDropBox[3] = timeEndDropBox3;
        timeEndDropBox[4] = timeEndDropBox4;
    }

Use an array as shown below:

public void addArrays(){
    DropBox[] dayDropBoxes = {dayDropBox0, dayDropBox1, dayDropBox2, dayDropBox3, dayDropBox4};
    for(int i=0; i < dayDropBoxes.length && i < dayDropBox.length; i++){
        dayDropBox[i] = dayDropBoxes[i];
    }
    //...
}

Note: I'm not sure the type of dayDropBox0, dayDropBox1 etc. Replace DropBox[] with the applicable type.


I assume you keep the fields in order to be able to access them all together, probably for something like for (JTextField textField : textFeilds).....

The way I do it is to use Arrays.asList and hold them into a List instead of an array. I get the advantage to stream them as well.

Example (assuming you want to clear the text of all textfields):

List<JTextField> textFields = Arrays.asList(field1, field2, field3, field4, field5);
textFields.stream().forEach(textField->textField.setText(""));