Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help adding actionlisteners

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
can sombody help me with how to add actionlisteners to the items I'm adding to the menu? thanks!

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class ApplicationList3 extends JFrame
{
    public ApplicationList3 ()
    {
        super ("Launcher");
        setSize (150, 50);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        
        //Store every application name in an array
        String AppNames[] = { "App1", "App2", "App3", "App4" };
        
        //Store every application path in an array
        File[] AppPaths = new File[5];
        
        AppPaths[0] = new File("http:test.jsp");
        AppPaths[1] = new File("C:\\test.mde");
        AppPaths[2] = new File("C:\\test2.mdb");
        AppPaths[3] = new File("C:\\test3.mde");
        
        //The third array, AppsLoaded, will only store those apps which are loaded on the machine and
        //menu items will be created from these
        String AppsLoaded[] = new String[5];
        
        //add applications that everybody has; don't need to check for
        AppsLoaded[0] = "App1";
        
        int i;
        for(i=1;i<4;i++){ //don't check App1, it's a web application, so everybody has it, so start at 1
        if(AppPaths[i].exists()){
            AppsLoaded[i] = AppNames[i];
            System.out.println(AppsLoaded[i]);}
        }
        
        JMenuBar jmb = new JMenuBar ();
        JMenu myMenu  = new JMenu ("Application");
        jmb.add(myMenu);
        JMenuItem [] menulist = new JMenuItem[10];
        
        for (i=0; i < 5; i++) {
        menulist[i] = new JMenuItem(AppsLoaded[i]); 
        myMenu.add(menulist[i]);
        }
        myMenu.addSeparator ();
        myMenu.add("Exit");

        setJMenuBar (jmb);
        setVisible (true);
    }
 
Try it with a tutorial from java.sun.com

---------------------------------------------
Yes, the world is full of strange people.
 
thanks, but I have spent many hours searching online trying to figure it out. The only samples I can find are to add it to an individual object, such as "button.addactionlistener" and I've tried many incarnations with my code but nothings worked. I can't find a tutorial which covers my case, in which I'm looping through a list and adding them. I only post a question after really trying to figure it out.
 
You can have the whole class ApplicationList3 implement the ActionListener interface:

Code:
public class ApplicationList3 extends JFrame implements java.awt.event.ActionListener

Each menu item then gets assigned the same listener (ApplicationList3) with the "this" operator:

Code:
  menulist[i] = new JMenuItem(AppsLoaded[i]);
  menulist[i].addActionListener(this);

Then create a method called actionPerformed to handle the events:

Code:
public void actionPerformed( java.awt.event.ActionEvent event )
{
   String command = event.getActionCommand();
   ...
}

Each menu item should return the menu text as the action command, or you can assign a different action command to each.
 
You just need to add an ActionListener - thats all there is to it ...

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;

public class ApplicationList3 extends JFrame implements ActionListener {

	public static void main(String []args) {
		new ApplicationList3();
	}


    public void actionPerformed(ActionEvent e) {
        JMenuItem source = (JMenuItem)(e.getSource());
        System.out.println(source.getText() +" clicked");
	}


    public ApplicationList3 ()
    {
        super ("Launcher");
        setSize (150, 50);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        //Store every application name in an array
        String AppNames[] = { "App1", "App2", "App3", "App4" };

        //Store every application path in an array
        File[] AppPaths = new File[5];

        AppPaths[0] = new File("http:test.jsp");
        AppPaths[1] = new File("C:\\test.mde");
        AppPaths[2] = new File("C:\\test2.mdb");
        AppPaths[3] = new File("C:\\test3.mde");

        //The third array, AppsLoaded, will only store those apps which are loaded on the machine and
        //menu items will be created from these
        String AppsLoaded[] = new String[5];

        //add applications that everybody has; don't need to check for
        AppsLoaded[0] = "App1";

        int i;
        for(i=1;i<4;i++){ //don't check App1, it's a web application, so everybody has it, so start at 1
        if(AppPaths[i].exists()){
            AppsLoaded[i] = AppNames[i];
            System.out.println(AppsLoaded[i]);}
        }

        JMenuBar jmb = new JMenuBar ();
        JMenu myMenu  = new JMenu ("Application");
        jmb.add(myMenu);
        JMenuItem [] menulist = new JMenuItem[10];

        for (i=0; i < AppsLoaded.length; i++) {
        	menulist[i] = new JMenuItem(AppsLoaded[i]);
        	menulist[i].addActionListener(this);
        	myMenu.add(menulist[i]);

        }
        myMenu.addSeparator ();
        myMenu.add("Exit");

        setJMenuBar (jmb);
        setVisible (true);
    }
}

--------------------------------------------------
Free Database Connection Pooling Software
 
mucho gracias sedj! looks like I was close...seems I had the listener below the .add(menulist) when it needs to be above

I had the actionperformed stuff at the bottom, but your way is certainly better since there's some variables I was wondering how I was going to reuse.
 
oh, and thanks idarke, looks like you had the same answer...all your replies are very appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top