aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Doubt in Observer pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Doubt in Observer pattern" Watch "Doubt in Observer pattern" New topic

Doubt in Observer pattern

Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367

I'm developing an applet for drawing rectangle with resize, color, borders etc

I have a ControlPanel for choosing tool ( Line, Oval, Rectangle ) and you can also choose color and bordercolor.

In the drawing area you can select multiple shapes resize them. When the shapes is selected I want to send updates to the controlpanel for updating the colors and I also want to send update message to some JSpinners that you also can use to resize the shapes with.

The problem is that the Update method of the Observer pattern needs to take several objects and the update in the JSpinner panel and the control panel need to take an array of active shapes. It seems rather tedious to passing objects and checking instance of in the update method

Isn't there a better approch for communication between GUI components. My old version put the JFrame, Panel in static variables so they could be accessed but I don't think this is a good approach. To have all Jframes, panels , dialogs in static variables.

Let me explain a problem

I have a color Picker for border colors and Background color for my shape classes. When a user clicks on the color in the controlpanel and chooses another color I want all my active shapes to update the colors. The same for the border. Problem is that the border color and the color is the exact same class. An TemplateColor class. When I pass the templatecolor class to the notifyObservers( myTemplateColor ) i can check instanceof in the update method but how will I know if it is regarding the border or the background? I could of subclass the TemplateColor but why? I must be missing something in this pattern. Can somebody please tell me on how to communicate properly between GUI classes. And don't want a reference to every class in every class that needs to be updated.

I agree. Here's the link:
subject: Doubt in Observer pattern