File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Resusing GUI code, different Events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resusing GUI code, different Events" Watch "Resusing GUI code, different Events" New topic

Resusing GUI code, different Events

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Ok, I thought that while I tried to come up with ideas of my own, I could go ahead and get this bruing in your head...
I have a dialog that has 3 JSliders..sliderX, sliderY, and sliderZ. These sliders are to manipulate an object on those axis. I also need to manipulate the camera on the same 3 axis so I thought it would be a good idea to reuse my GUI code, but I need to fire different events.
So what would be a good way to structure this? I don't really want to use the same class and have some sort of if/else in my ChangeListeners to determine if I am on camera or object. I would rather do something like extending the GUI part and each implementing their own events tha tI override.
Maybe I'm overthinking this? Anyone have suggestions on the approach I should take?

GenRocket - Experts at Building Test Data
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
I did something similar with a TreePopupMenu. And I started overthinking your problem.
All I did was create an abstract TreePopupMenu implementation that took care of the gui. I included one abstract method "showTreePopupMenu" that was called by the doMouseAction method of my implementation.
I actually posted my code here: Topic: JPopupMenu - setSelected(JMenuItem)
My subclasses implement the "showTreePopupMenu" differently, but use all the same code already built into the abstract class.
I thought about your situation initially like this:
- Create a custom interface like "ThreeWayController" that defines methods that do something with the ChangeEvent.
- In the Dialog, perhaps a constructor could accept an object that implements a ThreeWayController, or there could be a setter to put that into a global variable. Then your methods can just call the objects on the ThreeWayController, and the ThreeWayController takes care of the rest...
I would imagine a good foray into a patterns book would yield something helpful.
I agree. Here's the link:
subject: Resusing GUI code, different Events
It's not a secret anymore!