I have an ArrayList that needs to be read from and written to from multiple components of my GUI. I have drastically reduced the amount of code to try to illustrate the problem in this concise code segment below.
The parent frame might have a number of internal frames, and each internal frame will need its own instance of this ArrayList. However, all of the subcomponents of a specific internal frame will need access to the same instance of this ArrayList, so that additions and deletions are maintained in one true ArrayList for the specific internal frame. For this example, all the data in the ArrayList needs to be in memory. However, I will later add code to update a persistent data file every time a change is made in memory.
Here is my reduced code segment. Can anyone show me how to change this code so that it gives me the read/write access that I seek? Also, any links to relevant articles would be appreciated.
ParentFrame.java:
package testGlobalArrayList;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Panel;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JTabbedPane;
import java.util.*;
public class ParentFrame extends JFrame{
private static final long serialVersionUID = 1L;
JLayeredPane desktop;
JInternalFrame internalFrame;
public ParentFrame() {
super("parent frame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(600, 300));
Panel p = new Panel();
this.add(p, BorderLayout.SOUTH);
desktop = new JDesktopPane();
this.add(desktop, BorderLayout.CENTER);
this.pack();
this.setSize(new Dimension(600, 300));
this.setLocationRelativeTo(null);
final int DELTA = 40;
int offset = DELTA;
int ifWidth = 400;
int ifHeight = 200;
internalFrame = new JInternalFrame("internal frame", true, true, true, true);
internalFrame.setLocation(offset, offset);
offset += DELTA;
JTabbedPane jtp = createTabbedPane();
internalFrame.add(jtp);
// want to make this ArrayList read/write accessible to every GUI component below this level
ArrayList<Integer> myArrayList= new ArrayList<Integer>();
myArrayList.add(8);
myArrayList.add(6);
myArrayList.add(7);
desktop.add(internalFrame);
internalFrame.pack();
internalFrame.setSize(new Dimension(ifWidth,ifHeight));
internalFrame.setVisible(true);
}
private JTabbedPane createTabbedPane() {
JTabbedPane jtp = new JTabbedPane();
jtp.setMinimumSize(new Dimension(600,300));
createTab(jtp, "Tab1");
createTab(jtp, "Tab2");
return jtp;
}
private void createTab(JTabbedPane jtp, String s) {
if(s=="Tab1"){
TestGUI myTimeSeriesGUI = new TestGUI();
jtp.add(s,myTimeSeriesGUI);
}
else{jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));}
}
public static void main(String args[]) {
ParentFrame myParentFrame = new ParentFrame();
myParentFrame.setVisible(true);
}
}
TestGUI.java:
package testGlobalArrayList;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.Box;
public class TestGUI extends JPanel{
TestGUI(){
Box verticalBox = Box.createVerticalBox();
verticalBox.add(new TestPanel());
verticalBox.add(new TestPanel());
verticalBox.add(new TestPanel());
this.add(verticalBox, BorderLayout.CENTER);
}
void anotherMethod(){
// want to be able to add or delete records to same ArrayList here
myArrayList.add(5);
myArrayList.add(3);
myArrayList.add(0);
myArrayList.add(9);
}
}
TestPanel.java:
package testGlobalArrayList;
import java.awt.Color;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
public class TestPanel extends JPanel {
public TestPanel (){
this.setBackground(getRandomColor());
this.setBorder( new EtchedBorder() );
this.setSize(150,20);
}
void anotherMethod(){
//want to be able to add or delete records from same ArrayList here
myArrayList.remove(1);
myArrayList.remove(2);
myArrayList.remove(3);
}
private static Color getRandomColor(){
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b);
return randomColor;
}
}
pass the arraylist along in the constructors (and make it a field)
TestGui becomes
and TestPanel