Optimizing using static Popup Menu - How to do listeners?

Advertisement
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.

Advertisement

Replay

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)
<code>
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++)
registerThis(childs);
//Alt1: cast specifically
if (comp instanceof JLabel) {
((JLabel)comp).addMouseListener(this);
//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) {
ite.printStackTrace();
catch (IllegalAccessException iae) {
iae.printStackTrace();
* MouseListener methods
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
if (e.getSource() instanceof JLabel) {
myEventSource = (JLabel) e.getSource();
//Show popup
else {
//whatever
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
</code>
Thanks for the dukes ;-)
Regards,
rookie_no2

View 8 Replies

Tags:

  1. B8000_A422_000_085_140402_WW_WIFI
  2. vodafone plan tcode
  3. v7 things that we dont know about coc
  4. youtube does not open safari mac kids ç
  5. hp m1319f no print cartridge error
  6. ke30 report currency
  7. FM VIEW_MAINTENANCE_CALL.
  8. how to make my bt id as a primary bt id
  9. SAP PI CPIC-CALL: ThSAPCMRCV
  10. vendor mass confirmation sap
Copyrights 2019 Fcffair BigData Resource, All rights reserved