Results 1 to 4 of 4
  1. #1
    anilshelar is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default How to create ActionListners for dynamically created MenuItem?

    Hi Friends,
    I have to create menuitems dynamacally. I need help regaurding with how to create actionlistner for this menu item.

    my code is

    for (int i = 0; i < scanEntryList.size(); i++) {
    scanTmpl = (Notice) scanEntryList.get(i);
    //String senderid = scanTmpl.senderId;

    notices = new MenuItem(scanTmpl.senderId + ":" + scanTmpl.subject);

    ActionListener actionOnNoticeListner = new ActionListener() {

    public void actionPerformed(ActionEvent e) {

    String[] args = {scanTmpl.senderId, scanTmpl.subject, scanTmpl.dateTime.toString(), scanTmpl.contents};
    NoticeWindow.main(args);
    }
    };

    notices.addActionListener(actionOnNoticeListner);
    noticesMenu.add(notices);
    }

    here scanEntryList is Arraylist which contains message objects and object have senderId and subject...
    it create all menuitems but only action listners but all points to last entry
    so plz help me to create actionListner for all menuItems

    Regards
    Anil
    Last edited by anilshelar; 10-26-2009 at 01:42 PM.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    844
    Rep Power
    6

    Default

    Ur code is not clear.Paste the code code tag with what u are adding 2 listener.As per top level view ,u are adding to only one and not in loop.so the actionlistener is not fired for all menu items.
    Ramya:cool:

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Your code is unreadable as it is not properly formatted because you didn't use the "Code" tags. Edit you posting and post properly formatted code.

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Java Code:
    	for (int i = 0; i < scanEntryList.size(); i++) {
    		scanTmpl = (Notice) scanEntryList.get(i);
    		//String senderid = scanTmpl.senderId;
    		notices = new MenuItem(scanTmpl.senderId + ":" + scanTmpl.subject);
    		ActionListener actionOnNoticeListner = new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				[COLOR="DarkOrchid"]// evaluated when  actionPerformed() is called
    				String[] args = {scanTmpl.senderId, scanTmpl.subject, ...};[/COLOR]
    				NoticeWindow.main(args);
    			}
    		};
    		notices.addActionListener(actionOnNoticeListner);
    		noticesMenu.add(notices);
    	}
    This problem is an interesting exercise in determining when expressions are evaluated.
    Note that scanTmpl is declared outside the for-loop.
    When the body of the ActionListener is executed then -- and only then --
    the contents of String[]args is evaluated. So, yes, all the ActionListeners
    will use the last assigned value of scanTmpl .

    You could declare scanTmpl inside the for-loop:
    final Notice scanTmpl = ...
    A new instance of scanTmpl is created each time around the for-loop.
    It will have as its value the i'th element of scanEntryList.

    I would prefer to do the evaluation at the time the ActionListener is created:
    Java Code:
    	for (int i = 0; i < scanEntryList.size(); i++) {
    		scanTmpl = (Notice) scanEntryList.get(i);
    		//String senderid = scanTmpl.senderId;
    		notices = new MenuItem(scanTmpl.senderId + ":" + scanTmpl.subject);
    		ActionListener actionOnNoticeListner = new ActionListener() {
    			[COLOR="Blue"]// evaluated when this ActionListener is created
    			final String[] args = {scanTmpl.senderId, scanTmpl.subject, ...};[/COLOR]
    			public void actionPerformed(ActionEvent e) {
    				NoticeWindow.main(args);
    			}
    		};
    		notices.addActionListener(actionOnNoticeListner);
    		noticesMenu.add(notices);
    	}
    The declaration of args is evaluated only once,
    at the time of constructing the ActionListener.
    So each instance of ActionListener will have its own value of args.

Similar Threads

  1. [SOLVED] Can variable names be dynamically created?
    By CJSLMAN in forum New To Java
    Replies: 4
    Last Post: 01-03-2009, 02:06 AM
  2. Dynamically create a button, but what happened?
    By love2java in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 01:01 AM
  3. How to create widgets dynamically
    By sarbuland in forum Advanced Java
    Replies: 0
    Last Post: 02-06-2008, 09:08 PM
  4. create a tree when a new class is created
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 09:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •