aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes applying actionlistener to a dynamic submenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "applying actionlistener to a dynamic submenu" Watch "applying actionlistener to a dynamic submenu" New topic
Author

applying actionlistener to a dynamic submenu

Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
Project - the root folder
subfolders - under project folder
endfolders - under subfolders folder

i created dynamic JMenus(initialize in the constructor) from a subfolders of Project(this is the root folder), i made it dynamically because the folders are not permanent, it could be deleted, and also under subfolders is i created a JMenuItem which are the endfolders, now i am confused on how to add an actionlistener to the JMenu/JMenuItem because the initialization of the components are in my constructor,



that is how i initialize the JMenu dynamically, my problem is how to add an action listener to that style.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10375
    
    8

Subclass AbstractAction
Pass it to the constructor when you create the menus


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
can you give me some code snippet of that AbstractAction?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10375
    
    8



Use it with your code like

Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
hi maneesh, i tried your code but nothing happened after i click the submenu's. is there something i missed? it runs smoothly but no actionperformed happened.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10375
    
    8

Runs smoothly? No actionPerformed? What do you mean by runs smoothly then?
Impossible to say whats going wrong at your end without seeing the code.
Post some SSCCE which demonstrates the problem and which we can try out on our machines.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
shouldn't the submenu's be JMenuItem's
Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
@maneesh: i mean it run before, without the actionperformed, after i insert the code, it still run but no action when i click the JMenu, and yes i will post the code snippet but i have to make another because the original code is very long.. i will just make it short, regarding only to the topic..

@michael: it can be but theres an endfolders after subfolders, i will just show you the code..
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Following example works for me:

Like Maneesh suggested I create a custom action (SomeAction). In it's implementation you'd have to insert logic (what to do for different subfolders/menu items).


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
yes, i know now where i get stuck, because i put an action to a JMenu, instead that action will be on a JMenuItem
i wrote the action now in a JMenuItem and it works.

thanks maneesh for the code snippet, peter and michael.. it solves my problem

cheers to all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: applying actionlistener to a dynamic submenu