Results 1 to 5 of 5
  1. #1
    vandaloop is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default handling events of a class from another class

    Hi.

    I have aclass with a few text fields and buttons.

    I want to handle the events of this class from the calling (usualy a JFrame), as it is happens with the events of the standard components.

    How can I do this? I've googled a lot about it and even the few topics that really said something weren't too enlightnening.

  2. #2
    vandaloop is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    yeap, I figured it out. :)

    But I have a question. See:

    you declare your class in this case mPanel



    Java Code:
      
                  mPanel panel = new mPanel();
    and then you declare the method that will handle it and define the method
    Java Code:
     
     panel.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    Pressed(evt);
                }
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    Release(evt);
                }
    
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    NewJFrame.this.mouseClicked(evt);
                }
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    mouseEnter(evt);
                }
    
    
    
                private void Pressed(MouseEvent evt) {
                    jLabel1.setText("Press");
                }
    
                private void Release(MouseEvent evt) {
                    jLabel1.setText("Rel");
                }
    
                private void mouseEnter(MouseEvent evt) {
                    jLabel1.setText("ent");
                }
            });
                }

    My (newbish) question is how it understands from the generic "MouseEvent" which event happens every time to call the appropriate function? (press, release, etc)...
    It makes no check on it... :confused:

    For this one I used netbeans. If I switch environment must I do MouseEvent check first or it will work this way??

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm sorry, but I don't understand your question well. You may do well to restate it. Also, since you're learning Swing, a great resource that has helped me and innumerable others learn Swing is the Sun Swing Tutorial which you can find here: Swing Tutorial

    Much luck!

  4. #4
    vandaloop is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'm sorry, but I don't understand your question well. You may do well to restate it. Also, since you're learning Swing, a great resource that has helped me and innumerable others learn Swing is the Sun Swing Tutorial which you can find here: Tutorial[/URL]

    Much luck!
    if you mean the 2nd question nevermind. Lack of sleep. :p

    Thanks for the tut!

  5. #5
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

    Default

    Hey, the answer is either:

    (1) The class MouseEvent contains all variables & methods necessary to be any kind of Mouse Event, whether a click, or mouse press, or mouse release, etc.

    (2) MouseEvent might be extended by a class called MouseClickedEvent, or MouseReleasedEvent, or mouse MousePressedEvent. In this case, they would all be like different types of the MouseEvent object. So, they could be casted to a MouseEvent object before being passed into your handler methods.

Similar Threads

  1. Replies: 7
    Last Post: 05-06-2009, 01:42 PM
  2. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  3. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  4. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM
  5. Help, events in Jbutton class
    By Heather in forum Java Applets
    Replies: 2
    Last Post: 06-30-2007, 05:32 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
  •