问题描述:

I try to implements mechanism which will prevent before multiple clicks (I use popupListeners) on JComboBox and executing included for that event listeners.

For example:

public class SomeClass{

protected boolean semaphore = false;

public void initComboBox() {

JComboBox targetControllersComboBox = new JComboBox(); // combobox object

targetControllersComboBox.addPopupMenuListener(new PopupMenuListener() {

@Override

public void popupMenuWillBecomeVisible(PopupMenuEvent event) {

if (semaphore == false) {

semaphore = true; // here acquired semaphor

// HERE SOME CODE //

semaphore = false; // here release semaphor

}

}

}

}

I would like to avoid run code in popupListener before already running code in before executed popupListener. When popUplistener finish working than user can execute next popUplistener. My example unfortunately doesn't prevent about this situation. Anyone can help? I will great full for help.

UPDATE: FOLLOW (maris) RESLOVE PROBLEM:

 @Override

public void popupMenuWillBecomeVisible(PopupMenuEvent event) {

JComboBox comboBox = (JComboBox) event.getSource();

comboBox.removePopupMenuListener(this);

// some code ....

// now Listener is disabled and user cant execute next listener until this listener not stop working

comboBox.addPopupMenuListener(this); // after some code we add again listener, user now can use again listener

}

网友答案:

In general to avoid duplicate event firing during the process of event handling we can follow the below steps:

  1. Add Event listener with widget
  2. Once an event is fired, remove the event listener at the begging of the event handling
  3. Once event handling (business logic) is over, add the event listener

I am giving an skeleton example with JButton for your reference.

Eg:

JButton submit = new JButton(...)
submit.addActionListener(this);
...

public void actionEvent(...) {
    // on submit clicked
    submit.removeActionListener();
    // do the business logic
    submit.addActionListener(this);
}

Hope this will help you.

Regards,

maris

相关阅读:
Top