Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6785673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:09:21+00:00 2026-05-26T17:09:21+00:00

I have an ArrayList that needs to be read from and written to from

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:09:21+00:00Added an answer on May 26, 2026 at 5:09 pm

    pass the arraylist along in the constructors (and make it a field)

    public class ParentFrame extends JFrame{
        private static final long serialVersionUID = 1L;
        JLayeredPane desktop;
        JInternalFrame internalFrame;
        private List<Integer> myArrayList;
    
        public ParentFrame() {
            super("parent frame");
            myArrayList= new ArrayList<Integer>();
            myArrayList.add(8);
            myArrayList.add(6);
            myArrayList.add(7);
            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);
    
    
    
    
            desktop.add(internalFrame);
            internalFrame.pack();
            internalFrame.setSize(new Dimension(ifWidth,ifHeight));
            internalFrame.setVisible(true);
        }
    
    
        private void createTab(JTabbedPane jtp, String s) {
            if(s=="Tab1"){
                TestGUI myTimeSeriesGUI = new TestGUI(myArrayList);
                jtp.add(s,myTimeSeriesGUI);
            }
            else{jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));}
        }
    }
    

    TestGui becomes

    public class TestGUI extends JPanel{
    private List<Integer> myArrayList;
    
    TestGUI(List<Integer> myArrayList){
        Box verticalBox = Box.createVerticalBox();
        verticalBox.add(new TestPanel(myArrayList));
        verticalBox.add(new TestPanel(myArrayList));
        verticalBox.add(new TestPanel(myArrayList));
        this.add(verticalBox, BorderLayout.CENTER);
        this.myArrayList = myArrayList;
    }
    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);
    }
    

    and TestPanel

    public class TestPanel extends JPanel {
    private ArrayList<Integer> myArrayList;
    public TestPanel (ArrayList<Integer> myArrayList){
        this.setBackground(getRandomColor());
        this.setBorder( new EtchedBorder() );
        this.setSize(150,20);
        this.myArrayList = myArrayList;
    }
    void anotherMethod(){
        //want to be able to add or delete records from same ArrayList here
        myArrayList.remove(1);
        myArrayList.remove(2);
        myArrayList.remove(3);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stringtoList ArrayList that needs to return tokens from a StreamTokenizer but
I have an arraylist that contains items called Room. Each Room has a roomtype
I have an arraylist that gets different type of values in it, 1st value->
I have an ArrayList that I want to use to hold RaceCar objects that
I have an ArrayList<String> that I'd like to return a copy of. ArrayList has
I have structs like below and when I do that initialization: ArrayList nodesMatrix =
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the
In Java, say you have a class that wraps an ArrayList (or any collection)
I have a ArrayList made up of different elements imported from a db, made
I have an ArrayList<String> , and I want to remove repeated strings from it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.