robtothein
robtothein

Reputation: 23

Java Swing/awt - What happens after MousePressed till ActionEvent?

Is there a way to see what happens after mousePressed is called till ActionEvent is triggered? I need to create an UML sequence diagram, starting with mousePressed(MouseEvent) till the ActionEvent is triggered. Is there some documentation that shows this? I tried to debug an example in eclipse but for some reason I don't see when an ActionEvent is triggered.

jbutton.addMouseListener(new MouseListener() {
**THIS IS CALLED FIRST**
        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("pressed");
        }
    });



    jbutton.addActionListener(new ActionListener() {

        **AFTER SOME TIME THIS IS CALLED**
        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println("action");
        }

    });

Thanks

Upvotes: 0

Views: 68

Answers (1)

VGR
VGR

Reputation: 44414

Yes, there is documentation that shows this: ButtonModel.

From that page:

Pressing the mouse on top of a button makes the model both armed and pressed. As long as the mouse remains down, the model remains pressed, even if the mouse moves outside the button. On the contrary, the model is only armed while the mouse remains pressed within the bounds of the button (it can move in or out of the button, but the model is only armed during the portion of time spent within the button). A button is triggered, and an ActionEvent is fired, when the mouse is released while the model is armed - meaning when it is released over top of the button after the mouse has previously been pressed on that button (and not already released). Upon mouse release, the model becomes unarmed and unpressed.

As @dbrown93 points out, application code has no reason to add a MouseListener to a JButton. Remember there's other ways to activate a JButton, such as with the keyboard; no mouse activity is required.

Upvotes: 3

Related Questions