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!

Shortcut application (revisited)

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
I worked on this application a while ago and am now revisiting it; changed it around a bit. I'm loading file names & paths from a text file to populate a list of shortcuts. I've got that working; need some help on the action part. How do I tie together what item is clicked to the ActionListener part?

Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Shortcut extends JFrame {

	private JMenuItem itsInput = new JMenuItem();

	private List myList = new ArrayList();

	private static Map myMap = new HashMap();
	static {
//		store application paths
		myMap.put(new Integer(1), "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE");
		myMap.put(new Integer(2), "C:\\Windows\\EXPLORER.EXE");
		myMap.put(new Integer(3), "C:\\WINDOWS\\NOTEPAD.EXE");
		myMap.put(new Integer(4), "C:\\Program Files\\Microsoft Office\\Office10\\MSACCESS.EXE");

	}

	public Shortcut() {
		super("1STOP");
		setSize(300, 225);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		try {
			BufferedReader inputStream = new BufferedReader(new FileReader(
					"C:\\Main\\eclipse_workspace\\Shortcuts.txt"));
			String inLine = null;
			while ((inLine = inputStream.readLine()) != null) {
				String[] s = inLine.split(",");
				MenuConfig theMenuConfig = new MenuConfig();
				theMenuConfig.setId(Integer.parseInt(s[0]));
				theMenuConfig.setName(s[1]);
				theMenuConfig.setPath(s[2]);
				myList.add(theMenuConfig);
			}
			inputStream.close();
		} catch (IOException e) {
			System.out.println(e.toString());
		}

		ActionListener al = new MyListener();

		JMenu m1 = new JMenu("Apps");
		JMenu m2 = new JMenu("URLs");
		JMenu m3 = new JMenu("Folders");
		JMenu m4 = new JMenu("Files");

		for (int i = 0; i < myList.size(); ++i) {
			MenuConfig theMenuConfig = (MenuConfig) myList.get(i);
			System.out.println(theMenuConfig);
			JMenuItem aMenuItem = new JMenuItem(theMenuConfig.getName());
			aMenuItem.addActionListener(al);
			if (theMenuConfig.getId() == 99) {			//populate Apps
				m1.add(aMenuItem);
			} else if (theMenuConfig.getId() == 1) {	//populate URLs
				m2.add(aMenuItem);
			} else if (theMenuConfig.getId() == 2) {	//populate Folders
				m3.add(aMenuItem);
			} else										//populate Files
				m4.add(aMenuItem);
		}

		JMenuBar jmb = new JMenuBar();
		jmb.add(m1);
		jmb.add(m2);
		jmb.add(m3);
		jmb.add(m4);
		setJMenuBar(jmb);
		setVisible(true);
	}

	private class MyListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			//---NEED HELP HERE---
			// String cmd = e.getActionCommand();
			// try {
			// String[] commands = new String[] { AppPathHere, FilePathHere };
			// Process child = Runtime.getRuntime().exec(commands);
			// } catch (IOException a) {
			// System.err.println(a.getMessage());
			// }
		}

	}

	public static void main(String[] arguments) {
		Shortcut md = new Shortcut();
	}
}
----------------------------------------------------
public class MenuConfig {
	private int id;
	private String name;
	private String path;
	public MenuConfig() {

		
		
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
public String toString(){
	return id + ":" + name + ":" + path;
}
}
 
The numbers correspond to the first number that I'm reading in from the text file. In the example the number is 1. 1 corresponds to:
myMap.put(new Integer(1), "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE");

This way when I go to execute the link, I have the application path necessary to execute it.


example from the text file(AppID,Name,Path):
1,GOOGLE,
 
Also, I have a question on creating submenus. Figured I'd post here to save some space. I'm able to get the submenu name and one item added to it. Problem is, I'm reading line by line from a text file, so I'm getting mutiple submenus for the same one, each with it's own item. How can I add the items to just one submenu?

example:
Toplevel > Sublevel1
Toplevel > Sublevel2

should show as:
Toplevel > Sublevel1
Sublevel2

Code:
		for (int i = 0; i < myList.size(); ++i) {
			MenuConfig theMenuConfig = (MenuConfig) myList.get(i);
			System.out.println(theMenuConfig);
			JMenuItem aMenuItem = new JMenuItem(theMenuConfig.getName());
			aMenuItem.addActionListener(al);
			if (theMenuConfig.getId() == 99) { // populate Apps
				if (theMenuConfig.getLevel() == 1) { // check for submenu
					submenuName = theMenuConfig.getName();
					
				}else if(theMenuConfig.getLevel() == 2) { // check for submenu item
					JMenu optionMenu = new JMenu(submenuName);
					optionMenu.add(theMenuConfig.getName());
					m1.add(optionMenu);
				}
				else
					m1.add(aMenuItem);
			} else if (theMenuConfig.getId() == 1) { // populate URLs
				m2.add(aMenuItem);
			} else if (theMenuConfig.getId() == 2) { // populate Folders
				m3.add(aMenuItem);
			} else
				// populate Files
				m4.add(aMenuItem);
		}
 
No it is not, still need help with invoking the actionPerfrmed.

Sorry about the 2nd question, thought I'd keep it all together. I'll post that on it's own if I can't figure it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top