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

How do I disable the brushes popup menu when I left click in Photoshop CS5?

November 30

How do I disable the brushes popup menu when I left click in Photoshop CS5? I am trying to use the clone stamp and I am not able to get a sample because of this popup menu.LOL,  I can't even begin to complain with the number if times I misread questi

View All Replies

Encore CS4 crashes when export to image bluray disc (using popup menu)

November 30

Hi... When a export the bluray project to image bluray disc, its works fine. But, when i use popup menu, the exports crashs!! Please, help me. regards, AndreJeff, thx for reply. Yes, i create the popup menu in PS using template popup HD as model. Imp

View All Replies

How to listen mouse event when drag on popup menu

November 30

hi, all Please explain: When the Frame show, click mouse (left/right) on title bar of Frame, the popup menu will be show I Expected : When drag mouse on this popup menu will System.out.print("Mouse dragging on popup..."); Thanks many,I know this

View All Replies

How to run the oracle application cleint using static ip or domain please e

November 30

How to run the oracle application cleint using static ip or domain please explain me. i am not able to run oracle forms in client machine how to run it please explain me Please give me detail regarding that any body please help me. my mail id::::: [e

View All Replies

How to activate a menuitem on popup menu?

November 30

hello there i've made a popup menu with to menu items PopupMenu popup = new PopupMenu();                       MenuItem defaultItem2 = new MenuItem("Restore");               MenuItem defaultItem = new MenuItem("Exit");               po

View All Replies

Popup menue problem

November 30

when adding popup menues on text pane component then try to appeare it ,the textpane component appeare in front of the popup menue i want fix this problem!!!!!Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/

View All Replies

Cutom colors to popup menu

November 30

Hi all, I need to install custom look and feel for my popup menu, the following code works fine most of the time but still it turns back to the default colors some time, can any one tell me why this behaviour. Also when a menuitem is selected a grey

View All Replies

Disable Right Mouse Button Popup Menu

November 30

How can I disable right mouse button popup menu for a LabVIEW control to use my custom popup menu? Alessio ColziSorry for the stupid question but I forgot that option. Thank you Gorka Alessio "Gorka Larrea" ha scritto nel messaggio news:[email p

View All Replies

Challenge popup menu,  css stylesheet and button in table component

November 30

Hello, in my last post i put the code of an example of popup menu but the only thing that need to run was a cdata tag. But in the example i only can open and close the popup menu in the top and left location that previously set in the css stylesheet

View All Replies

Problems with Popup Menu on Mac

November 30

Hi, i've created an application which displays a Popup menu. i've used me.isPopupTrigger() to display the popup.it works well on windows. but when i run the same on Mac OS-X .it is not at all showing the Popup menu. is there any way i can solve this.

View All Replies

Help...popup menu problem

November 30

problem with popup menu i've designed the poopmenu in firworks...imported the file into dreamweaver...but when mouse is over the menu the drop down menu goes behind the flash animation placed below it.....how to fix?????:All Active content on a page

View All Replies

Focus lost when a popup menu is activated...

November 30

It seems that when a popup menu is activated the component invoker lost focus. this is ugly because a focus lost can trigger unexpected events (formatting, validating, etc). how can I avoid this? thanxI must seem a bit weird being the only one to rep

View All Replies

Addin popup menu

November 30

i have a JList contained in a JScrollPAne. HOw do i add a popup menu to the items within the list. When i right click the menu should popup. I tried adding the popup menu using scrollpane.add(popup); It s not responding to the rght clicksUsually, pop

View All Replies

Mail app closes when not used for a while how fix?

November 30

mail app closes when not used for a while how fix?This may also be due to the Sudden Termination feature built into OS X, which will effectively quit a process if the system is demanding resources. Mail can be a RAM hog at times, and if your system o

View All Replies

Using a popup and PASS/FAIL Criteria

November 30

How do you use a popup to query the user for PASS/FAIL criteria? I have an LED ON/OFF. Want to use POPUP to ask user condition. If LED is in an incorrect state, seq fails. Want to allow user 2nd chance in case wrong button is hit the 1st time.Look at

View All Replies

Popup Menu simple question

November 30

Hi ive got my applet and I want to respond with a popup menu when the user right clicks Will java.awt.popupmenu provide a popup menu with the look and feel of the OS's popup menus? Im using a simple canvas in my applet. Is this the best class to do a

View All Replies

Popup menu shadows are gone

November 30

''Duplicate post, continue here - [https://support.mozilla.com/en-US/questions/795121]'' just fresh-installed ff4 rc and noticed that popup menu shadows are gone. Is it expected behavior?Are you using DMX6.0 or 6.1? If the former, please update to th

View All Replies

Popup menu stalls page

November 30

I was wondering if anyone had any suggestions. I had my popup menu exported to dreamweaver and it was all working. I then added more javascript to the page and now its not working. I exported it all again and inserted it but no luck. The moment i rol

View All Replies

Problem with Pre-Popup-Menu in Hierarchical Tree

November 30

Hi, In Forms 10g I have made an application with an Hierarchical Tree. All works well, except the use of the Pre-Popup-Menu trigger. In that trigger when I read the values of Node_Label and Node_Value I get the values of the previous selected node, n

View All Replies

Tags:

  1. YouTube não carrega no lg d300f
  2. BExGetData
  3. CL_SQL_RESULT_SET=============CP
  4. cisco wlc dynamic interface
  5. does STATUS_CHANGE_EXTERN COMMIT WORK
  6. UAFT
  7. QRRI
  8. NL3I
  9. vendor mass confirmation sap
  10. 4Z1A
Copyrights 2019 Fcffair BigData Resource, All rights reserved