想写一个editable JcomboBox,当focus gained时候从后台读文件,更新Jcombobox的下拉单选项;当从下拉单选择一个选项或者手动编辑的时候,做一系列其他操作。现在的问题是,当focus gain的时候,同时会执行actionPerformed(),我想在从下拉单选择选项的时候才激发actionPerformed(),不知各位达人有没有办法?

解决方案 »

  1.   

    試試  先remove掉focus監聽   然後再在適當的時候add上來
      

  2.   

       这样有问题,我检查了一下,执行的顺序是:
                 enter focusGained();
                enter actionPerformed();
                exit actionPerformed();
                enter actionPerformed();
                exit actionPerformed();
               exit focusGained();
       如果是把鼠标移动到别处:
                  enter actionPerformed();
                exit actionPerformed();
                enter actionPerformed();
                exit actionPerformed();
                enter focusLost();
                exit focuslost();
       太诡异了
                 
      

  3.   

     上面的执行顺序是我从代码中打印出来的,如果要removeFocusListener,太繁琐了,很容易出bug
      

  4.   

    那就在頁面初始化的時候 就把Jcombobox的值也帶出來~ 何必還要作個focus呢?
      

  5.   

      focus还要做其他的辅助功能 我的想法是在actionperformed执行时在开始处 block focus事件,不知可不可行?