简体   繁体   中英

Populating 2D array from dynamic TextField After Event (Java)

I have a java UI with 3 TextFields, when a button is clicked the inputs are taken and assigned to corresponding columns in the array. How do I iterate this so that I can populate the array with more than 1 set of inputs:

https://i.stack.imgur.com/Zw1U6.png

I have a working action listener for when the button is clicked:

nextActivity.addMouseListener(new java.awt.event.MouseAdapter() {
   public void mouseClicked(java.awt.event.MouseEvent evt) {
      MouseClicked(evt);}
   private void MouseClicked(MouseEvent evt) {

      String activityName = activityNameField.getText();
      String activityDuration_String = activityDurationField.getText();
      String dependantActivities = activityPredeField.getText();

But how do I return a populated Object array 'rowData[][]'?

As per the UI shared by you and your requirement, I would like to recommend you to use Next Activity button's Action Performed method which is quite easy and simple to implement.

For storing your data, as @DevilsHnd mentioned in the comment, you at least need to know the number of rows to initialize a 2D array in Java.

(I don't think you need to know the column size initially, you can initialize the column size as and when needed)

Now probably the best way to store data here is using an ArrayList class of the List Interface .

In my solution below, I have used ArrayList for storing the multiple sets of inputs.

As you have used an array in your code, I thought to come up with a solution where we can use an array to store a single input (3 text-fields data).

class data {
    String information[];

    data(String input[]) {
        information = input;
    }

    @Override
    public String toString() {
        return "Activity Name: " + information[0] + ", Duration: " 
                + information[1] + ", Predecessors: " +information[2];
    }
}

ArrayList<data> Input = new ArrayList<>();

private void nextActivityActionPerformed(java.awt.event.ActionEvent evt) {                                             

    // Take data from the text fields..
    String activityName = activityNameField.getText();
    String activityDuration_String = activityDurationField.getText();
    String dependantActivities = activityPredeField.getText();

    // Store this data in an array of size 3..
    String input[] = new String[3];
    input[0] = activityName;
    input[1] = activityDuration_String;
    input[2] = dependantActivities;

    // Store it in our global list..
    Input.add(new data(input));

    // set text fields empty so that new input can be taken..
    activityNameField.setText("");
    activityDurationField.setText("");
    activityPredeField.setText("");
}                                            

private void doneActionPerformed(java.awt.event.ActionEvent evt) {                                     

    // Take data from the text fields..
    String activityName = activityNameField.getText();
    String activityDuration_String = activityDurationField.getText();
    String dependantActivities = activityPredeField.getText();

    // A condition to check if the last entry was left taken as an input..
    boolean condition = (activityName.equals("")) && (activityDuration_String.equals("")) 
                        && (dependantActivities.equals(""));

    // if all 3 inputs are not empty then..
    // You can change as per your requirements....
    if(!condition) {                                
        // Store this data in an array of size 3..
        String input[] = new String[3];
        input[0] = activityName;
        input[1] = activityDuration_String;
        input[2] = dependantActivities;

        // Store it in our global list..
        Input.add(new data(input));

        // set text fields empty so that new input can be taken..
        activityNameField.setText("");
        activityDurationField.setText("");
        activityPredeField.setText("");
    }

    for(data inp : Input) 
        System.out.println(inp);        // Just print the stored data...
}          

Note:

1) Data class stores a single set of input.

2) Input list will store all the input sets taken before pressing "Done" button.

3) At last, all data entered is printed in the "Done" button's Action Perfomed method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM