Brandon Nguyen
Brandon Nguyen

Reputation: 345

Java Detect MouseClick anywhere in Window doesn't work in certain areas

So I have a Java Swing program and I want to be able detect mouse clicks.

addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        update(evt); //another method in the program
    }
});

The code works if I click on the window's side or places where there isn't an object, but does not work when I click on objects in the JFrame, such as my JTable or my text field.

Please help me how to have the MouseListener work on objects inside the JFrame as well.

Upvotes: 0

Views: 783

Answers (1)

Vince
Vince

Reputation: 15156

When you click on a textfield, the textfield gains focus. That means your frame loses ownership of focus, and since your listener is most likely added to your frame, your listener stops working right when frame isnt in focus. Add your listener to all compoments, or use Key Binding

Upvotes: 1

Related Questions