Wrathchild
Technical User
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;
}
}