• Post Reply Bookmark Topic Watch Topic
  • New Topic

Communication between GUI classes  RSS feed

Brian Johnsonmyer
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am struggling with GUI design regarding the best way to encapsulate and enable communication between a customJFrame, customContentPane, customJMenu, mouseHandler, and graphicsData.

My customJFrame instantiates customContentPane, customJMenu, and mouseHandler.

My customJMenu.actionPerformed() instantiates graphicsData and reads the file into graphicsData when a user opens a file from customJMenu.


1. How can customJMenu get customContentPane to paint graphicsData when customJMenu does not have a reference to customContentPane?

2. How can mouseHandler.mousePressed() select a line drawn on customContentPane using graphicsData, remove the line from graphicsData, and get customContentPane to repaint?


I tried putting customJFrame, customContentPane, customJMenu, mouseHandler in a single class using inner classes. Pro: eliminates many access issues between the objects, Con: makes for a long and overly broad class (in my opinion).

I also tried passing references of objects during instantiation. Pro: enables access between objects, Con: it seemed like I was adding unnecessarily dependencies between objects because if I wanted Ob3 to be able to call methods from ObX in the case where Ob1 instantiates Ob2, which in turn instantiates Ob3, I had to pass the ObX reference to Ob3 via Ob1 and Ob2, even though Ob1 and Ob2 had no need for ObX.

I tried making graphicsData a Singleton. Pro: can access graphicsData from anywhere using static call. Con: I may want multiple graphicsData instances to enable display multiple sets of graphics.

I tried making graphicsData a static variable in customJFrame. Pro: can access graphicsData from anywhere using static call. Con: I have been told that it is bad practice to allow outside classes to modify a class variable directly.

I am thinking about making a static method in each class to return a reference to the class/object using keyword this and providing accessor methods. Pro: enable global access to objects Con: I think this may generate compiler errors since I do not see how a static method can return a reference to a single object if multiple objects can be instantiated.

I found an example containing a single file which had practically every variable in the program listed as a static. Pro: he could access any object or variable from anywhere in the program; Con: this seems to go against every thing I have been taught about object oriented programming

Is there a “right way” to encapsulate these classes and allow them to communicate?

Is there a good example that is simple, but complex enough to show interaction between a data module, content pane, JMenu, and mouse? For instance, opening a graphics file, drawing the graphics file, selecting graphics to edit, updating the data module, and redrawing the graphics.

Thank you!!!
Stephan van Hulst
Saloon Keeper
Posts: 6981
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian, take a look at this skeleton:
Let me know if it helps you get ideas for your project.
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!