I am having a problem in which I am losing data in an arrayList. After I call the method Refresh in class MPUComp I go into the class mpuChecker and call updateTextArea.
By doing this I am losing the data that existed in the arraylist in MPUComp. What I am doing wrong. I think it has to do with how I call the class. How do I correctly keep this data?
public class MPUComp extends JFrame {
{
private mpuChecker mC;
public ArrayList<String> oldTags = new ArrayList<String>();
public void menu()
{
//...
class MenuActionListener3 implements ActionListener {
public void actionPerformed(ActionEvent e)
{
mC = new mpuChecker();
mC.CheckMpu(path, textField.getText(),1);
setVisible(false);
}
}
class MenuActionListener4 implements ActionListener {
public void actionPerformed(ActionEvent e)
{
mC = new mpuChecker();
mC.CheckMpu(path2, textField_1.getText(),2);
setVisible(false);
}
}
public void refresh(String pane1) {
textArea_1.append(pane1 + "\n");
System.out.println(getOldTags().size());
System.out.println(oldTags.size());
//both print out zero when called second
}
public void updateTextArea(final String text) {
textArea_2.append(text + "\n");
oldTags.add(text);
System.out.println(oldTags.size());
//prints out the correct arraylist size
}
}
}
//second class which calls updateTextArea and refresh
public class mpuChecker {
private MPUComp mC = new MPUComp();
public void CheckMpu(String path, String searchToken, int form)
{
// Print the text to the appropriate text-area either 1 or 2
public void ary1(int path)
{
if(path == 1)
{
for(int l = 0; l < midTags.size(); l++)
{
mC.refresh(midTags.get(l));
}
}
if(path == 2)
{
for(int lk = 0; lk < midTags2.size(); lk++)
{
mC.updateTextArea(midTags2.get(lk));
}
}
}
}
Following up on what jpm suggested, to avoid this you could do
in MPUComp. That way you only instantiate that mpuchecker once. Both ActionListeners could then use that MPUChecker.
If you want each ActionListener to have its own MPUChecker, you could move the creation of their listeners into the body of those internal classes, like so
On the other hand, MPUChecker itself might be referring to the wrong MPUComp, since you create one for the MPUChecker at initialisation of that object. Unless that is the intended behavior, you could remove
from MPUChecker, make CheckMPU static and give it an additional parameter: the MPUComp it’s supposed to check.