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 7557701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:12:04+00:00 2026-05-30T12:12:04+00:00

Here is the problem I’m running into – I have a GUI class implementing

  • 0

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!

  • 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-30T12:12:05+00:00Added an answer on May 30, 2026 at 12:12 pm

    Some recommendations:

    • Yes use a for loop to eliminate 90% of your redundant code and to make debugging and modifying (and getting others to read and understand your code) a lot easier.
    • More importantly I think that you will need to pass references for this to work, specifically a reference to the visible GUI object to the code that is supposed to be doing the loading since you will want the changes to effect the currently visible JFrame, correct?
    • Give this GUI class public methods that allow other objects to set the background color of its contents. This should be a non-constructor method.

    Edit 1
    Some other points:

    I have a class dedicated to saving and loading.

    Good as you want to separate this out from GUI (or “view”) code.

    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.

    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:

    public class SaveLoad { // or whatever its called
      private GUI gui;  // give it a GUI variable 
    
      public SaveLoad(GUI gui) {
        this.gui = gui;  load in the current GUI into save load
        // ... other code ...
      }
    
      public void load() {
        // get the colors
        gui.loadColors(...); // method called on the visualized GUI.
    

    Then you’d create SaveLoad like this perhaps:

    public void actionPerformed(ActionEvent evt) {
      // inside some event listener
      SaveLoad saveload = new SaveLoad(GUI.this);  
    
      // or just this if not in an inner class then just use this
      // SaveLoad saveload = new SaveLoad(this);  
      // .....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a strange problem here... I have an ASP.NET 3.5 application that has
I have a problem here. I'm creating UILabels dynamically to be displayed into a
Unusual problem here: I have an app that uses a text file which contains
My big problem here is that I'm 1 day into learning MacOSX so the
Weird problem here, we're running a few mixed environment web applications, that use Windows
I have a problem here. My Zend_Forms do not render in view script. Via
I've got a problem here with an MSI deployment that I'm working on (using
I have an interesting problem here I've been trying to solve for the last
My problem here is that I would like to pass an object to a
I have problem here, I need all the stuffs inside this $_SESSION['cart'] thing and

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.