简体   繁体   中英

Java: mouseClicked event not always firing

I have the following code to listen for mouseclicks, but it appears to not always fire.

nameList.addChangeListener( new MouseAdapter(){
    @Override
    public void mouseClicked(MouseEvent me) {
        nameListUpdated();
    }
} );

nameListUpdated() will run some of the times, but for whatever reason it seems that it will not run on every click like i want. Does anyone know why? Or is there more information to find the problem?

I would suggest instead of using method addChangeListener

 nameList.addChangeListener( new MouseAdapter(){
 @Override
 public void mouseClicked(MouseEvent me) {
     nameListUpdated();
 }
} );

use method for adding mouse listener addMouseListener

 nameList.addMouseListener( new MouseAdapter(){
 @Override
 public void mouseClicked(MouseEvent me) {
     nameListUpdated();
 }
} );

Try using mousePressed instead. It will fire an event when you press mousebutton without waiting until you release it. In most cases it does the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM