Results 1 to 15 of 15
- 06-28-2011, 02:18 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
How to pass events up a GUI hierarchy
What's the best way to pass events up a GUI (Swing) hierarchy?
For example, say you had an application class MainGUI, which contains an instance of another class MainPanel,
which contains an instance of another class SmallPanel. Let's say you want the MainGUI object to do something
when the instance of the SmallPanel object was clicked with the mouse.
Java Code:class MainGUI { MainPanel mainPanel; //do something if the SmallPanel object is clicked ... } class MainPanel { SmallPanel smallPanel; ... } class SmallPanel { public void MouseClicked(MouseEvent e) { //clicked } ... }
- 06-28-2011, 02:32 AM #2
Sounds like the place for a listener. The MainGUI object should add a listener to the SmallPanel object.you want the MainGUI object to do something when the instance of the SmallPanel object was clicked with the mouse.
- 06-28-2011, 02:51 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
So are you saying that inside the MainGUI class I should have:
Which would mean I would need access to the SmallPanel instance.Java Code:smallPanel.addMouseListener(...);
It seems weird to have the event handling code for the SmallPanel class in another class.
- 06-28-2011, 02:56 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Think of how you would add functionality to a button. The approach should be identical(or very similar), since a button is just a smaller GUI component.
- 06-28-2011, 02:57 AM #5
I didn't say that the SmallPanel shouldn't handle the event.have the event handling code for the SmallPanel class in another class.
What kind of notification do you want the small panel to send to the MainGUI panel? Is it like an event?
Yes if the MainGUI class needs to be notified of an event in the SmallPanel class.I would need access to the SmallPanel instance.
- 06-28-2011, 03:13 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
Okay that makes sense to me.
Maybe I worded that wrong. The SmallPanel as I have it right now implements the MouseListener and MouseMotionListener interface
and supports selection and dragging with the mouse. Should I just move all these listener methods (MouseClicked(), MousePressed(), etc) to the MainGUI class?
- 06-28-2011, 03:17 AM #7
Which class handles the event? If the SmallPanel doesn't do anything then I'd say move them to the class that does.Should I just move all these listener methods
As sunde887 said, treat SmallPanel as a special type of button.
- 06-28-2011, 03:20 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Something like
Since you want to add the listening to the small component, and the small component handles the mouse movement.Java Code:small.addMouseListener(small)
- 06-28-2011, 03:28 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
- 06-28-2011, 03:34 AM #10
What notification does the SmallPanel need to pass to the MainGUI class? I assume it's not the MouseClick event.
I'm proposing a new event that the MainGUI would listen for. The SmallPanel would notify its listeners when this new event should passed.
- 06-28-2011, 03:38 AM #11
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
- 06-28-2011, 03:46 AM #12
Sounds like a TimeToChangeImageEvent.
There was a similar thread to this one in the past couple of weeks. It referred to an existing mechanism in Java to handle new user defined events. I don't remember what the name of the class was. But it handled all the bookkeeping.
Myself, I like to roll-my-own: Define a new TimeToChangeEvent class, an addTimeToChangeImageListener() method, a ChangeImageListener interface.
When the event occurs in SmallPanel, create a new TimeToChangeImageEvent, and call all the listeners on the list saved by the addTimeToChangeImageListener.
And bob's your uncle.Last edited by Norm; 06-28-2011 at 04:00 AM. Reason: Add SmallPanel processing
- 06-28-2011, 05:20 AM #13
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
Would this be a bad idea?
Java Code:class MainGUI { MainPanel mainPanel; setViewportImage(BufferedImage img) {...} ... } class MainPanel { SmallPanel[] smallPanels; ... } class SmallPanel { BufferedImage image; public void MouseClicked(MouseEvent e) { MainPanel mainPanel = (MainPanel) getParent(); MainGUI gui = (MainGUI) mainPanel.getParent(); gui.setViewportImage(this.image); } ... }
- 06-28-2011, 02:37 PM #14
Yes, it locks your GUI structure to the current layout.
- 06-28-2011, 03:14 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
STRUTS 2 project folder hierarchy
By buyapentiumjerk in forum Web FrameworksReplies: 3Last Post: 06-18-2011, 05:50 PM -
catching exceptions (hierarchy)
By kailash in forum Java ServletReplies: 2Last Post: 02-10-2011, 06:43 AM -
Hierarchy in Project
By efozdel in forum New To JavaReplies: 4Last Post: 07-23-2010, 09:49 AM -
Java Documentation Class Hierarchy Browser (I'd like to find one :\)
By Bat0u89 in forum New To JavaReplies: 6Last Post: 07-04-2010, 04:01 PM -
typed events vs untyped events.
By Drun in forum SWT / JFaceReplies: 0Last Post: 11-23-2009, 12:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks