Here is the problem I’m running into – I have a GUI class implementing JFrame with a constructor that builds the Frame with 9 Panels in a 3×3 GridLayout. Each Panel is initialized in this constructor and have their own listeners, etc. However, there is a menu option to Load a file to display, but in order for me to be able to save/load files, I have a class dedicated to saving and loading. I’ve tested it out, and the problem lies in that when the load method in the save/load class is called, it creates a GUI object, hence re-making GUI components. When the GUI object is used to invoke a method called loadedFile in GUI (GUI.loadedFile), the program is supposed to set each JPanel to a certain RGB value background. However, it does not update my JPanel’s backgrounds. Here is the part of constructor which initializes the panels and the loadedFile code:
A1 = new JPanel();
A1.addMouseListener(mouseListener);
A1.setBackground(Color.WHITE);
add(A1);
A2 = new JPanel();
A2.addMouseListener(mouseListener);
A2.setBackground(Color.WHITE);
add(A2);
A3 = new JPanel();
A3.addMouseListener(mouseListener);
A3.setBackground(Color.WHITE);
add(A3);
B1 = new JPanel();
B1.addMouseListener(mouseListener);
B1.setBackground(Color.WHITE);
add(B1);
B2 = new JPanel();
B2.addMouseListener(mouseListener);
B2.setBackground(Color.WHITE);
add(B2);
B3 = new JPanel();
B3.addMouseListener(mouseListener);
B3.setBackground(Color.WHITE);
add(B3);
C1 = new JPanel();
C1.addMouseListener(mouseListener);
C1.setBackground(Color.WHITE);
add(C1);
C2 = new JPanel();
C2.addMouseListener(mouseListener);
C2.setBackground(Color.WHITE);
add(C2);
C3 = new JPanel();
C3.addMouseListener(mouseListener);
C3.setBackground(Color.WHITE);
add(C3);
System.out.println("GUI() invoked");
}
loadedFile:
public void loadedFile(int[] colors) {
int counter = 0;
//if in a different pain program using JPanels in an array for larger canvases,
//use the JPanel[counter] set to colors[counter] for BG color. Also, enhanced
//for loop could cycle through he values of panels array and set BG.
A1.setBackground(new Color(colors[counter], colors[counter+1], colors[counter+2]));
counter+=3;
A2.setBackground(new Color(colors[counter], colors[counter+1], colors[counter+2]));
counter+=3;
A3.setBackground(new Color(colors[counter], colors[counter+1], colors[counter+2]));
counter+=3;
B1.setBackground(new Color(colors[counter], colors[counter+1], colors[counter+2]));
counter+=3;
B2.setBackground(new Color(colors[counter], colors[counter+1], colors[counter+2]));
counter+=3;
B3.setBackground(new Color(colors[counter], colors[counter+1], colors[counter+2]));
counter+=3;
C1.setBackground(new Color(colors[counter], colors[counter+1], colors[counter+2]));
counter+=3;
C2.setBackground(new Color(colors[counter], colors[counter+1], colors[counter+2]));
counter+=3;
C3.setBackground(new Color(colors[counter], colors[counter+1], colors[counter+2]));
System.out.println("BGS SET");
}
Thanks in advance to anyone who can help!
Some recommendations:
Edit 1
Some other points:
Good as you want to separate this out from GUI (or “view”) code.
This is what I’m trying to get you to avoid. Instead give your Save/Load class a constructor that accepts the GUI as a parameter and use this GUI object (not a newly created one) to do your owrk.
Edit 2
Please read up and comply with Java naming conventions. In particular, class names should begin with a capital letter and variable and method names should begin with a lower case letter. This may seem trivial at first, but if one gets used to seeing code this way for several years, it makes it much easier to understand someone else’s code (which is often a difficult process made much more difficult with your current naming scheme).
Edit 3
More specifically, I’m recommending something like this:
Then you’d create SaveLoad like this perhaps: