Optimizing using static Popup Menu - How to do listeners?

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)
<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

Similar Messages

  • DW & FW8 Popup Menu Relative Positioning

    I can tell there's been a lot of discussion here about using,
    or not using, DW popup menus. Here's my situation: I'm successfully
    (really) using a popup menu on a left-justified site. We've now
    added a blog to the site. That blog page is centered. Of course,
    the popup doesn't line up properly like it does on the rest of the
    site. I'm told there's a way to position the menu relative to it's
    trigger, and not by xy coordinates in DW8. Can anyone verify that,
    and, if so, tell me how to do it?
    Thanks much,
    Jan

    > I'm successfully (really) using a popup
    > menu on a left-justified site.
    So you think. But do you know that your site will not be
    spidered if that's
    your only navigation? And do you know that the menu will
    break if you move
    or rename a linked file? And do you know that your menu
    doesn't work if js
    is disabled? And do you know that your menu is inaccessable?
    > Can anyone verify that
    It's possible to make the layers that are written dynamically
    by the pop-up
    menu code center, yes.
    > tell me how to do it?
    I am reluctant to encourage anyone to continue using these
    menus. But
    consider this -
    Change this -
    </head>
    to this -
    <style type="text/css">
    <!--
    body { text-align:center; color:#CCC; }
    #wrapper { text-align:left; width:720px; margin:0
    auto;position:relative; }
    -->
    </style>
    </head>
    change this -
    <body ...>
    to this -
    <body ...>
    <div id="wrapper">
    and this -
    </body>
    to this -
    </div><!-- /wrapper -->
    </body>
    and see if that helps.
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.dreamweavermx-templates.com
    - Template Triage!
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    http://www.macromedia.com/support/search/
    - Macromedia (MM) Technotes
    ==================
    "jknewlegend" <[email protected]> wrote in
    message
    news:e7pgo2$b4t$[email protected]..
    >I can tell there's been a lot of discussion here about
    using, or not using,
    >DW
    > popup menus. Here's my situation: I'm successfully
    (really) using a
    > popup
    > menu on a left-justified site. We've now added a blog to
    the site. That
    > blog
    > page is centered. Of course, the popup doesn't line up
    properly like it
    > does
    > on the rest of the site. I'm told there's a way to
    position the menu
    > relative
    > to it's trigger, and not by xy coordinates in DW8. Can
    anyone verify
    > that,
    > and, if so, tell me how to do it?
    >
    > Thanks much,
    > Jan
    >

  • Oscontrol popup menu

    Hello All,
    I have recentley discovered a problem with the oscontrol
    xtras popupmenu. I have found after populating the list with more
    than 256 items you are unable to select any past the 256th!!
    Does anyone know about this, is there a fix? I'm in trouble
    here as i've used this xtra within database programs i have wrote,
    when the database starts filling up this dropdowns going to be
    useless!!
    Please help
    Thanks
    LD

    The Apple Human Interface guidelines strongly recommend
    against using a popup menu to display more than 256 items. If you
    need to display that many items, it is better to use a scrolling
    list, a treeview or a hierarchical menu. The OSControl Xtra
    provides an OSmenu member which you can use to create up to four
    hierarchical levels. With as few as 16 items per level, you can
    display over 65,000 items.
    Tabuleiro provides a
    TreeView
    Xtra.
    You can create a scrolling list with multiple selections,
    using the technique shown
    here.
    If you really do want to show more than 256 items using the
    OSControl popupMenu, contact me privately.

  • How to disable the run-time popup menu in the sequence display on TestStand Operator Interface, that allow the use to skip the test?

    How to disable the run-time popup menu in the sequence display on TestStand Operator Interface, that allow the use to skip the test?

    Hello,
                  Regarding the skip/force pass/force fail options, when I set the ControlExecFlow to True in Teststand 3.1 and 3.5, in the Sequence Editor the menu options for skip/force pass/force fail are not active for Technician but, when I launch the Operator Interface logged with Technician the options are active. The problem is that if the technician sets one step to one of these options, and change the user to Operator, the test step remains skip/force pass.
                  Is there any possibility without modifying the Operator Interface (at programming level), to reload default values of the steps when changing the user to Operator?
    Thank you,
    Best regards,
    paio

  • How to get system popup menu in java?

    I want to know how to get windows xp or any operating systems default popupmenu in java.
    And i also have problem for using mnemonic for JPopupMenu, it works well when i first press right click of my mouse and use the specified mnemonic for that the action specifed for that is triggered but when is use mnemonic without the using my mouse it doesn't trigger why.

    Please how to get native(e.g windows explorer) popup menu in jfilechooser

  • In iCloud Numbers how do I set up a popup menu in an entire column

    I have a database using iCloud Numbers. The table is for my tax deductions for 2014. In one column I have Doctors/Dentists/etc. Instead of typing that title each time I create a new entry in that column, how can I set a popup menu like I can in Excel and choose Doctors/Dentists/etc?

    Hi Aunest,
    You have couple of options here.
    You can make a copy of the site-wide template for every catalogue and build the menu manually.
    The other option is to use:
    {module_cataloguelistdump,-1,rowLength,targetFrame,sortBy}
    Displays the List of all Catalogues as names that are links to those catalogues
    Parameters:
    Use -1 to display parent catalogs, use -2 to display all catalogs including sub-catalogs or a catalogue ID to display the subcatalogues of a particular catalogue
    rowLength - will limit the number of items per row when items are displayed as a list. Default is 1 item per row.
    targetFrame - e.g. _blank. Specify the frame you want the item to open in
    sortBy - will sort the web apps in specified order
    Alphabetical - sorts items alphabetically
    Weight - sorts items by weight
    Example
    {module_cataloguelistdump,-1,1,_blank,Alphabetical} - this module will display the catalogue list in the new window, alphabetically and all in one column
    Cheers,
    -mario

  • How to convert text values in a popup menu to numerical values?

    Hi there,
    I am trying to create a table in Numbers where one cell needs to have a numerical value inserted into it based upon the value chosen in a popup menu in another cell, and I'm not sure how to go about it.
    To be more specific, I have a cell containing a popup menu with the items "Bronze", "Silver", "Gold" and "Platinum". Depending on which value is chosen, I need another cell to be populated with a value of 1,1.5,2 or 2.5 respectively, as I will then use this value to perform a multiplication elsewhere in the table. I have tried using the IF function in the 2nd cell but I can only get that to match one value and not an array of values.
    Could anybody advise on the most effective way of accomplishing what I am trying to do?
    Many thanks in advance.

    Michael Quayle wrote:
    (1) Hopefully you're not looking for points for being helpful, because all you're doing here is patronising me from the looks of it.
    "The fact to be a newbie doesn't prevent someone to have a quick look, at least to the list of available functions, to know what may be done with the tool."
    (2) This was *obviously* the first thing I did, followed by a Google search. When those avenues were exhausted I tried here. However, like I said, having "a quick look" is useless if you are not familiar enough with spreadsheets to know what it is you are looking for! MY choice of language to describe what I was trying to achieve was obviously not compatible with any search I performed, which is why I had to resort to asking a person rather than a manual. And frankly, the language used within the documentation is often rather alien to me. Having read the documentation for VLOOKUP I still don't fully understand its application. I'm a musician for heaven's sakes - everybody knows we can't read!
    "The users guides were designed to help users, not to help helpers to help users "
    (3) Are all your posts this condescending? I came here to ask a question, not to be accused of being lazy and not reading documentation. Like I said, my own searches through the manual were fruitless, so I came here for help. Are all the responses on this forum so cold and terse?
    (1) Don't worry, I'm not here to grab points.
    (2) Searching in the existing threads with the available tool seems more logical than searching in Google.
    (3) I write what I want, the way I want.
    In the cell C2 of the main table, the formula is :
    =IF(ISBLANK(B2),"",VLOOKUP(B2,vlook_up :: A:B,2,FALSE))
    Apply fill down to fill other cells.
    Yvan KOENIG (VALLAURIS, France) jeudi 4 août 2011 12:55:03
    iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0
    My iDisk is : <http://public.me.com/koenigyvan>
    Please : Search for questions similar to your own before submitting them to the community
    To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

  • How to inform user that there is an attached popup menu....

    Hi ,
    I have some popup menus on some text items......
    Until now , i have inserted a hint which is displayed automatically when the user activates these text items ..... but the hint is dispayed at the bottom... and the user may not see it....
    How can i make it more 'visible'....?????
    I use Dev10g
    Many thanks,
    Simon

    Use the WHEN-NEW-ITEM-INSTANCE and if the item has a
    popup menu change the colorBut then you'll have to change it back again when the user leaves the field, which I don't believe is possible when you have list items (especially in multi-record blocks) because of when the when-new-item-instance trigger fires for these items. Even at the pjc level poplists behave strangely with regards to their focusGained (or whatever it's called) event.

  • How to change the selection background color of the selected item in the popup menu of the choice box ?

    How to change the selection background color of the selected item in the popup menu of the choice box ?
    By defaut, the selection background color likes "blue", but if I want it to be "yellow" for example, how should I do ?
    Thanks

    The id is applied by (I think) the skin class of the ChoiceBox. You don't need to define it.
    You have to apply the css in an external style sheet. You can apply the external style sheet to any parent of your choice box, or to the scene (the most usual way to do it).
    Example:
    import java.util.ArrayList;
    import java.util.List;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.ChoiceBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    public class ChoiceBoxTest extends Application {
      @Override
      public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Example 2");
        final ChoiceBox<String> choiceBox = new ChoiceBox<>();
        List<String> tempResult = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
          tempResult.add("Item " + i);
        choiceBox.getItems().setAll(tempResult);
        VBox root = new VBox();
        root.getChildren().add(choiceBox);
        final Scene scene = new Scene(root, 300, 250);
        scene.getStylesheets().add("choiceBox.css");
        primaryStage.setScene(scene);
        primaryStage.show();   
      public static void main(String[] args) {
        launch(args);
    choiceBox.css:
    @CHARSET "UTF-8";
    #choice-box-menu-item:focused  {
    -fx-background-color: yellow ;
    #choice-box-menu-item .label {
    -fx-text-fill: black ;
    Message was edited by: James_D

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

    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 questions, and then try to break my own record for the number of typos in a single post.
    The image above was taken on a photgraphic weekend where the group (of all young ladies ) I was travelling with, set us a photography challenge.  The one above was my response to a 'faceless portrait'.  The only other one I attempted was this response for a picture of a 'wild animal', featuring camera club president Liz.
    Oh what fun we had, laughing and laughing until our faces fell off.
    [EDIT]  Both taken at Karamea, which some visitors to NZ might recognise as the western end of the Heaphy Track.

  • How to add click popup menu to a row of a table.

    hi,
    I have a JTable with say 10 rows of data.
    On selecting a particular row and user right clicks then a popup menu should come.
    I dont want to attach popup to the JTable,instead i want it to be attached to the rows of the JTable.
    How to do it??
    Thanks & regards
    Neel

    You have no choice but to "attach" it to the JTable; it is the JTable which is
    receiving the events that you would use to trigger the popup.
    That said, there is nothing stopping you from displaying a different popup
    depending on which row was clicked on.
    : jay

  • How to connect my ipad using static ip

    How to connect my ipad using static ip?
    Using Netgear Router.

    Go to Setting > General > Network > WiFi > Then tap the blue icon next to your network, from here you can setup a static IP on your internal network.

  • My movie has clips that have some extracted audio that I would like to use for the menu music in iDVD 11. Does anyone know how to do this? Copy and paste does not seem to work.

    My iMovie 11 movie has clips that have some extracted audio that I would like to use for the menu music in iDVD 11. Does anyone know how to do this? Copy and paste does not seem to work.

    I would use Quicktime Pro.
    See: QuickTime Pro: How to Extract or Disable Tracks at http://docs.info.apple.com/article.html?artnum=42596 and QuickTime Player 7 Help - Extracting, Adding, and Moving Tracks at http://docs.info.apple.com/article.html?path=QuickTime%20Player/7/en/c3qt6.html
    To get Quicktime 7.6.6 for Snow Leopard see: QuickTime Player 7.6.6 for Mac OS X v10.6.3 at http://support.apple.com/kb/DL923  (Quicktime 7.6.6 is the latest version.)
    To upgrade to Quicktime Pro see: QuickTime Player 7 Help - Getting QuickTime Pro at http://docs.info.apple.com/article.html?path=QuickTime%20Player/7/en/c2qt.html

  • How to specify term file when using static html to run webforms 10g

    I'm using static HTML to run forms
    document.writeln('<PARAM NAME="serverArgs" ');
    document.writeln(' VALUE="escapeParams=true module=genoa.fmx userid= sso_userid=%20 sso_formsid= sso_subDN= sso_usrDN= debug=no host= port= term=/forms90/hsd65html/genoa.res"> ');
    I'm specifying the term as term=/forms90/hsd65html/genoa.res.
    During forms startup I get the message unable to read the file. I can bwoense the file by http://server/forms90/hsd65html/genoa.res
    Any idea how to specify the term file in static html.
    Thanks

    term is not a http address, but a directory address. So, you have to specify the path on the server:
    term=c:\oracle\forms90\.....

  • After the last update of Firefox, my toolbar menu became blank but the bookmarks are still available from a pulldown menu. I want the on the toolbar like they used to be. How do I recover this?

    After the last update of Firefox, my toolbar menu became blank but the bookmarks are still available from a pulldown menu. I want the on the toolbar like they used to be. How do I recover this?

    ''Attaboyslim wrote:''
    I am locked into firefox.
    If nothing happens when you press the F11 key, check if you have the same problem in Safe Mode.
    * [[Troubleshoot Firefox issues using Safe Mode]]
    ''Attaboyslim wrote:''
    I'd like the old browser please.
    * [[How to make the new Firefox look like the old Firefox]]

Maybe you are looking for