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

  • Home
  • SEARCH
  • 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 8890459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:30:25+00:00 2026-06-14T22:30:25+00:00

So my Java program has a Main class that creates and initializes the Game

  • 0

So my Java program has a Main class that creates and initializes the Game object. The Game class contains the code that handles the procession of my program. My Game class initializes my Display class. My Display class initializes my DisplayPanel class. My DisplayPanel class implements MouseListener.

My problem is that I want to send the MouseListener data back to the Game object on demand. But I can’t access my the game object properly from DisplayPanel because it is contained within the Main class and method.

I can’t think of a proper way to work around this. I’m sure this issue and issues like this are fairly common and would appreciate to know what the common and proper way to handle this issue and ones like this is.

I will post code if you really think it’s necessary but I feel like I’ve explained everything you really need to know and it would be a waste of my time.

  • 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-06-14T22:30:26+00:00Added an answer on June 14, 2026 at 10:30 pm

    As established in the comments:

    The easiest way of getting this to happen would be in the Game class, like so:

    public class Game {
        private MouseListener mouseListener = new MouseListener() {
            public void onClick(MouseEvent e) {
                // application logic here
            }
    
            public Game() {
                DisplayPanel dp = new DisplayPanel();
                dp.addMouseListener(mouseListener);
            }
        };
    }
    

    Then, you simply direct it to the DisplayPanel class through a series of method calls:

    public class DisplayPanel extends JPanel {
        public void addMouseListener(MouseListener mouseListener) {
            this.addMouseListener(mouseListener);
        }
    }
    

    (Note that in this case, since JPanel has an addMouseListener method, implementing your own is unnecessary.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If a class has inheritance in a Java program, do I need to put
I am making a simple Java program for class that is supposed to output
I wrote a Java program that has 3 classes. When, I use javac, I
I have a small Java program that reads list of IPs from text file
I have a java program that performs 5 different tasks. When I run the
I'm calling a java program from my python code in the following way: subprocess.check_output([java,
I have developed a Java program that will count the number of files in
I have a Java program that reads some text from a file, line by
I'm trying to create a Java program that flips two coins to see who
I am writing a program that connects to a web host that has a

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.