java - Show popup menu on a jlist -
i have problem popup menu. that want when user click right mouse button on jlist popup menu appear. have created class create popup menu, class extend mouselistener, , class add mouse listener jlist. in class extend mouselistener call class of popup menu , show it. problem popup menu doesn't appear.
package mouselistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.*; import view.___poupupmenu___; public class add_popupmenu_categoria implements mouselistener { jlist <string> l = new jlist <string> (); public add_popupmenu_categoria (jlist <string> l) { this.l = l; } public void mouseclicked(mouseevent evt) { system.out.println("clicked"); if (evt.ispopuptrigger()) { system.out.println("enter in clicked"); ___poupupmenu___ p = new ___poupupmenu___(); l.setselectedindex(l.locationtoindex(evt.getpoint())); system.out.println(evt.getcomponent()); l.setcomponentpopupmenu(p.menu_categoria); p.menu_categoria.show(evt.getcomponent(), evt.getx(), evt.gety()); } } public void mouseentered(mouseevent e) { } public void mouseexited(mouseevent e) { } public void mousepressed(mouseevent evt) { system.out.println("pressed"); if (evt.ispopuptrigger()) { system.out.println("enter in pressed"); ___poupupmenu___ p = new ___poupupmenu___(); l.setselectedindex(l.locationtoindex(evt.getpoint())); system.out.println(evt.getcomponent()); l.setcomponentpopupmenu(p.menu_categoria); p.menu_categoria.show(evt.getcomponent(), evt.getx(), evt.gety()); } } public void mousereleased(mouseevent evt) { system.out.println("released"); if (evt.ispopuptrigger()) { system.out.println("enter in released"); ___poupupmenu___ p = new ___poupupmenu___(); l.setselectedindex(l.locationtoindex(evt.getpoint())); system.out.println(evt.getcomponent()); l.setcomponentpopupmenu(p.menu_categoria); p.menu_categoria.show(evt.getcomponent(), evt.getx(), evt.gety()); } } } package view; import javax.swing.*; import java.awt.*; public class ___poupupmenu___ { public jpopupmenu menu_categoria = new jpopupmenu(); public jpopupmenu menu_scuola = new jpopupmenu(); public jpopupmenu menu_maschile_femminile = new jpopupmenu(); public jpopupmenu menu_dirigenti_allenatori = new jpopupmenu(); public jpopupmenu menu_img_profilo = new jpopupmenu(); jmenuitem menu_elimina = new jmenuitem("elimina"); jmenuitem menu_modifica = new jmenuitem("modifica"); jmenuitem menu_apri = new jmenuitem("apri..."); jmenuitem menu_pagamento = new jmenuitem("visualizza pagamenti"); jmenuitem menu_genitore = new jmenuitem("visualizza genitore"); jmenuitem menu_visita_medica = new jmenuitem("visualizza scadenza visita medica"); public ___poupupmenu___ () { menu_elimina.setfont(new font("segoe ui", 1, 15)); menu_modifica.setfont(new font("segoe ui", 1, 15)); menu_apri.setfont(new font("segoe ui", 1, 15)); menu_pagamento.setfont(new font("segoe ui", 1, 15)); menu_genitore.setfont(new font("segoe ui", 1, 15)); menu_categoria.add(menu_modifica); menu_categoria.add(menu_elimina); menu_scuola.add(menu_apri); menu_scuola.add(menu_visita_medica); menu_scuola.add(menu_genitore); menu_scuola.add(menu_pagamento); menu_scuola.add(menu_modifica); menu_scuola.add(menu_pagamento); menu_maschile_femminile.add(menu_apri); menu_maschile_femminile.add(menu_visita_medica); menu_maschile_femminile.add(menu_modifica); menu_maschile_femminile.add(menu_elimina); menu_dirigenti_allenatori.add(menu_apri); menu_dirigenti_allenatori.add(menu_modifica); menu_dirigenti_allenatori.add(menu_elimina); } } package controller; import javax.swing.*; import mouselistener.add_popupmenu_categoria; public class categoriacontroller { public void add_popupmenu_categoria (jlist <string> l) { add_popupmenu_categoria apmc = new add_popupmenu_categoria (l); l.addmouselistener(apmc); } }
when click is:
pressed released enter in released javax.swing.jlist[,0,0,897x797,alignmentx=0.0,alignmenty=0.0,border=,flags=50331944,maximumsize=,minimumsize=,preferredsize=,fixedcellheight=-1,fixedcellwidth=-1,horizontalscrollincrement=-1,selectionbackground=javax.swing.plaf.coloruiresource[r=184,g=207,b=229],selectionforeground=sun.swing.printcoloruiresource[r=51,g=51,b=51],visiblerowcount=8,layoutorientation=0] clicked
someone can me?
edit: simplest way use setcomponentpopupmenu
method of jlist:
jpopupmenu popup = new jpopupmenu(); popup.add(new jmenuitem("hello world")); jlist<string> jlist = new jlist<string>(); jlist.setcomponentpopupmenu(popup);
setcomponentpopupmenu(popup)
appends popup component. show automatically if right-click component. no mouselistener needed.
another easy way implement popup-menus swing components override getcomponentpopupmenu()
method of jcomponent. solution this:
public class myjlistwithpopupmenu extends jlist { //... @override public jpopupmenu getcomponentpopupmenu() { // create popupmenu return myjpopupmenu; } //... }
when use myjlistwithpopupmenu
instead of regular jlist, popup wil show in right place when right-click on it.
Comments
Post a Comment