Results 1 to 15 of 15
  1. #1
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default 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
      }
    
      ...
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    you want the MainGUI object to do something when the instance of the SmallPanel object was clicked with the mouse.
    Sounds like the place for a listener. The MainGUI object should add a listener to the SmallPanel object.

  3. #3
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    So are you saying that inside the MainGUI class I should have:
    Java Code:
    smallPanel.addMouseListener(...);
    Which would mean I would need access to the SmallPanel instance.

    It seems weird to have the event handling code for the SmallPanel class in another class.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    have the event handling code for the SmallPanel class in another class.
    I didn't say that the SmallPanel shouldn't handle the event.
    What kind of notification do you want the small panel to send to the MainGUI panel? Is it like an event?
    I would need access to the SmallPanel instance.
    Yes if the MainGUI class needs to be notified of an event in the SmallPanel class.

  6. #6
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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.
    Okay that makes sense to me.

    Quote Originally Posted by Norm View Post
    I didn't say that the SmallPanel shouldn't handle the event.
    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.
    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?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Should I just move all these listener methods
    Which class handles the event? If the SmallPanel doesn't do anything then I'd say move them to the class that does.
    As sunde887 said, treat SmallPanel as a special type of button.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Something like
    Java Code:
    small.addMouseListener(small)
    Since you want to add the listening to the small component, and the small component handles the mouse movement.

  9. #9
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Which class handles the event? If the SmallPanel doesn't do anything then I'd say move them to the class that does.
    As sunde887 said, treat SmallPanel as a special type of button.
    The SmallPanel handles the event. My setup right now has the SmallPanel handling its own events.
    The MainPanel actually creates many SmallPanel instances, so where would I put the SmallPanels' event-handling code
    so that the MainGUI can be updated?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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.

  11. #11
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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.
    Each SmallPanel contains a BufferedImage. When the SmallPanel is clicked its BufferedImage should be displayed elsewhere in the MainGUI.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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

  13. #13
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default

    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);
      }
    
      ...
    }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Yes, it locks your GUI structure to the current layout.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by Norm View Post
    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.
    There's the old Observer/Observable setup, but I find them a bit clunky.
    If there's a newer one then I haven't bumped into it.
    Like you I tend to roll my own, since the Observable bit isn't exactly complex.

Similar Threads

  1. STRUTS 2 project folder hierarchy
    By buyapentiumjerk in forum Web Frameworks
    Replies: 3
    Last Post: 06-18-2011, 05:50 PM
  2. catching exceptions (hierarchy)
    By kailash in forum Java Servlet
    Replies: 2
    Last Post: 02-10-2011, 06:43 AM
  3. Hierarchy in Project
    By efozdel in forum New To Java
    Replies: 4
    Last Post: 07-23-2010, 09:49 AM
  4. Replies: 6
    Last Post: 07-04-2010, 04:01 PM
  5. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 12:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •