Optimizing using static Popup Menu - How to do listeners?

November 30

Ok, I just did a little profile of my application, and realized a bit of a problem. I have a large number of items subclassing JLabel. Each has a popup menu, with, say, 5 JMenuItems. This leads to a whole lot of JMenuItems, even though they are all the same. So my first thought was to make a single, static JPopupMenu.
Now the place where differentiated behavior should occur is in the listener that listens to the menu items. The data a listener needs to display when an event occurs is needed is specific to the instance of the label.
Basically, what I'd like to know is, is there a good way to cut down on instances of the menu items while keeping the necessary functionality.
Let me know any ideas you have, or get me to clarify anything, and I'll try.



You are right, JLabel also inherits from Container..maybe i should look into the hierachy before writing such things...
first: i reworked the code a bit so now it should work
second: is the popupmenu the only source which invokes actionevents?
then you could remember the eventsource when the popupmenu is invoked (in mouseListener) and use it when a actionevent occurs (see attribute myEventSource)
btw. you must register the popupmenu with the overseer too because it is a container "outside" your panel (no direct child)
public class MyOverseer implements ActionListener, MouseListener {
JLabel myEventSource = null;
public void registerThis (Component comp) {
//recurse subcomponents
if (comp instanceof Container) {
Component[] childs = ((Container) comp).getComponents();
for (int i=0; i<childs.length; i++)
//Alt1: cast specifically
if (comp instanceof JLabel) {
//Alt2: using reflection
Class thisClass = this.getClass();
Class[] implementedInterfaces = thisClass.getInterfaces();
for (int i=0; i<implementedInterfaces.length; i++) {
Class aInterface = implementedInterfaces[i];
String interfaceName = aInterface.getName();
if (interfaceName.endsWith("Listener")) {
//Name of the addXYZListener-Method
String addMethodName = "add"+interfaceName;
try {
//get class of current component
Class componentsClass = comp.getClass();
//get the add-method
Method addMethod = componentsClass.getMethod(addMethodName, new Class[] {aInterface});
//invoke the add-method on the current comp and pass THIS as argument
addMethod.invoke(comp, new Object[] {this});
catch (NoSuchMethodException nsme) {} //can't add this Listner
catch (InvocationTargetException ite) {
catch (IllegalAccessException iae) {
* MouseListener methods
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
if (e.getSource() instanceof JLabel) {
myEventSource = (JLabel) e.getSource();
//Show popup
else {
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
* ActionListener methods
public void actionPerformed (ActionEvent e) {
if (myEventSource!=null) {
//do something
//after that
myEventSource == null; //just as before
Thanks for the dukes ;-)

View 8 Replies


  1. will z10 pair with ive-w530
  2. apple tv hulu no signal
  3. Nokia 6120c midp certificate download
  4. F990
  5. tipwss
  6. happilyd55
  7. leathero71
  8. day597
  9. pack27y
  10. press7s1
Copyrights 2019 Fcffair BigData Resource, All rights reserved