简体   繁体   中英

Data assigned to new multidimensional array keeps updating

sorry I don't know how title this question but I am having an issue with assigning data to a new multidimentional array, do something and then retrieve the old data.

In my case I am trying to loop through JTextFields, assign all current data specifically the colour to a new multidimentional array. Then I want to do a search and change the background colour of found textfields.

Now I have a reset button that I would like the old colours that are now in the new multidimentional array to be assigned back to the fields. The issue I am having is that the new multidimentional array has updated after the search with the new colours. I really appreciate if someone can point me to the right direction.

Here is my code:

public JTextField[][] fields = new JTextField[totalX][totalY];
    public JTextField[][] newFields = new JTextField[totalX][totalY];
    
    if (e.getSource() == btnFind  || e.getSource() == txtSearch)
    {
        // Make a copy of fields before selecting everything
        for(int t = 0; t < totalX; t++){
            for(int r = 0; r < totalY; r++){
                newFields[t][r] = fields[t][r];
            }
        }
        
        findStudentRecord();
        // when looping though newFields[x][y] here it is already updated to the current colour
        
    }
    if (e.getSource() == btnReset)
    {
        for(int x = 0; x < totalX; x++){
            for(int y = 0; y < totalY; y++){
                fields[x][y].setText(newFields[x][y].getText());
                fields[x][y].setBackground(newFields[x][y].getBackground()); 
                // have tried this one but doesn't work
                if(newFields[x][y].getBackground() == Color.green){
                    fields[x][y].setBackground(Color.green);
                    System.out.print(fields[x][y].getText() + "\n");
                }
            }
        }
    } 

Here is the findStudentRecord()

public void findStudentRecord()
{
    boolean found = false;
    String strFind = txtSearch.getText();
    for(int x = 0; x < totalX; x++){
        for(int y = 0; y < totalY; y++){
            if(fields[x][y].getText().equalsIgnoreCase(strFind))
            {
                found = true;
            }
        }
    }
    if (found)
    {
        for (int x = 0; x < totalX; x++)
        {
            for(int y = 0; y < totalY; y++){
                if(fields[x][y].getText().equalsIgnoreCase(strFind))
                {
                    fields[x][y].setBackground(new Color(255,217,200));
                }
            }
        }
        txtSearch.setText(txtSearch.getText() + " ...Found.");
    }
    else
    {
        txtSearch.setText(txtSearch.getText() + " ...Not Found.");
    }
}

You have to create a new JTextField and add the relevant data:

    newFields[t][r] = new javax.swing.JTextField();
    newFields[t][r].setText(fields[t][r].getText());
    newFields[t][r].setColumns(fields[t][r].getColumns());
    // any other property you want to transfer

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