jmenuitem - How to refresh item in a popup menu? -


i have popup menu (that comes out when user uses right click on specified elements), wich items readed list. want when item selected, item disabled in popupmenu (then if action happen return enabled). have implemented popupmenu, cannot implement enable/disable jmenuitem element. can me? thanks

    class popuptriggerlistener extends mouseadapter {       public void mousepressed(mouseevent ev) {         if (ev.ispopuptrigger()) {           menu.show(ev.getcomponent(), ev.getx(), ev.gety());           x = ev.getx();           y = ev.gety();         }       }        public void mousereleased(mouseevent ev) {         if (ev.ispopuptrigger()) {           menu.show(ev.getcomponent(), ev.getx(), ev.gety());           x = ev.getx();           y = ev.gety();         }       }        public void mouseclicked(mouseevent ev) {       }     }    }        jlabel label = new mylabel("right-click");          public test() {             setdefaultcloseoperation(jframe.exit_on_close);             jmenuitem item = new jmenuitem("test1");             item.addactionlistener(new actionlistener() {               public void actionperformed(actionevent e) {          system.out.println("menu item test1");         jlabel newlabel = new jlabel("test");         label.add(newlabel);         newlabel.setbounds(x, y, 40, 10);               }             });       menu.add(item);        item = new jmenuitem("test2");       item.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {           system.out.println("menu item test2");         }       });       menu.add(item);        getcontentpane().add(label);       pack();       setsize(300, 100);     }      public static void main(string[] args) {       new test().setvisible(true);     } 

the way done using actions. actions extensions of actionlistener interface. can set action of, example, jmenuitem , in action can set enabled false. automatically disable jmenuitem. alternately can enable setting enabled true on action. here action api @oracle: action api java

and here discourse on how use actions: how use actions java


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -