java - How to override the JPopupMenu method show? -
my goal highlight jlist item after rightclick show jpopupmenu.. read advice overriding method show way.. in case declare jpopupmenu @ beginning of class
jpopupmenu popupmenu = new jpopupmenu(); jmenuitem masterlist,settings;
then have method set menuitems
public void setpopupmenu(int depth//ignore var){ //popupmenu = new jpopupmenu(); popupmenu.removeall(); masterlist = new jmenuitem("masterlist"); settings = new jmenuitem("settings"); //action marsterlist masterlist.addactionlistener(new actionlistener(){ //stuff here } }); //action settings settings.addactionlistener(new actionlistener(){ //stuff here } }); popupmenu.add(masterlist); popupmenu.add(settings); }
and list in different method
list.setselectionmode(listselectionmodel.single_selection); list.setlayoutorientation(jlist.horizontal_wrap); list.setvisiblerowcount(-1); list.setcomponentpopupmenu(popupmenu);
i tried putting on mouseadapter of list popupmenu fires first , ignores highlighting...
if ( swingutilities.isrightmousebutton(mouseevent) )//highlight right clicked item { int row = list.locationtoindex(mouseevent.getpoint()); list.setselectedindex(row); string val = (string)list.getselectedvalue(); system.out.println(val); }
i know overriding this
popupmenu = new jpopupmenu(){ @override public void show(){} }
but cant because manipulating menuitems on method... or there other approach can suggest...
rather trying modify state of jpopupmenu
, why not modify state of menu item when detect right click...
so, basically, make use of actions api define menu item jpopupmenu
, allows register listselectionlistener
underlying jlist
...
public class showitemaction extends abstractaction { private jlist list; public showitemaction(jlist list) { this.list = new jlist(); putvalue(name, "showing ..."); list.addlistselectionlistener(new listselectionlistener() { @override public void valuechanged(listselectionevent e) { if (!e.getvalueisadjusting()) { int index = list.getselectedindex(); string value = list.getselectedvalue().tostring(); putvalue(name, "showing " + value + " @ " + index); } } }); } @override public void actionperformed(actionevent e) { // actual action performed when selected } }
all is, when selection changed, change text (name
) of action, changes text of menu item, based on selected row.
when create jlist
, assign jpopupmenu
via setcomponentpopupmenu
method, means no longer need care , displayed appropriately based on current , feel requirements
jlist list = new jlist(model); jpopupmenu popupmenu = new jpopupmenu(); popupmenu.add(new showitemaction(list)); list.setcomponentpopupmenu(popupmenu);
i add mouselistener
jlist
use change row selection when right click on jlist
...
list.addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { jlist list = (jlist)e.getcomponent(); if (swingutilities.isrightmousebutton(e)) { int row = list.locationtoindex(e.getpoint()); list.setselectedindex(row); } } });
this not entirely fool proof, if right click jlist
while popup visible, mouselistener
doesn't appear notified :p
runnable example...
import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.actionevent; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import javax.swing.abstractaction; import static javax.swing.action.name; import javax.swing.defaultlistmodel; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jpanel; import javax.swing.jpopupmenu; import javax.swing.jscrollpane; import javax.swing.swingutilities; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.listselectionevent; import javax.swing.event.listselectionlistener; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } defaultlistmodel model = new defaultlistmodel(); model.addelement("one"); model.addelement("two"); model.addelement("three"); model.addelement("four"); jlist list = new jlist(model); jpopupmenu popupmenu = new jpopupmenu(); popupmenu.add(new showitemaction(list)); list.setcomponentpopupmenu(popupmenu); list.addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { if (swingutilities.isrightmousebutton(e)) { int row = list.locationtoindex(e.getpoint()); list.setselectedindex(row); } } }); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new jscrollpane(list)); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class showitemaction extends abstractaction { private jlist list; public showitemaction(jlist list) { this.list = new jlist(); putvalue(name, "showing ..."); list.addlistselectionlistener(new listselectionlistener() { @override public void valuechanged(listselectionevent e) { if (!e.getvalueisadjusting()) { int index = list.getselectedindex(); string value = list.getselectedvalue().tostring(); putvalue(name, "showing " + value + " @ " + index); } } }); } @override public void actionperformed(actionevent e) { } } }
Comments
Post a Comment