Wrathchild
Technical User
I have an program that adds an item to a list if the user has the corresponding application loaded on their machine. My problem is if the user doesn't have the application it leaves a blank line. I believe the problem is with the line "String AppsLoaded[] = new String[8];" as I'm setting the length prior to really figuring out what the user has loaded. What's the best way to solve this? Should I remove a blank entry after the fact or should I not reserve a set amount of entries in AppsLoaded to begin with?
thanks!
thanks!
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class ApplicationList extends JFrame implements ActionListener {
//store Access & Internet Explorer paths
String MSACCESS = "C:\\Program Files\\Microsoft Office\\Office10\\MSACCESS.EXE";
String IE = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";
public static void main(String []args) {
new ApplicationList();
}
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
String cmd = source.getText();
HashMap apps = new HashMap ();
apps.put ("CNN", "[URL unfurl="true"]http://cnn.com");[/URL]
apps.put ("db1", "C:\\db1.mde");
apps.put ("db2", "C:\\db2.mde");
apps.put ("db3", "C:\\db3.mde");
apps.put ("db4", "C:\\db4.mde");
apps.put ("db5", "C:\\db5.mde");
apps.put ("db6", "C:\\db6.mde");
apps.put ("db7", "C:\\db7.mde");
if (cmd.equals ("Exit")){
System.exit(0);
}
String path = (String)apps.get (cmd);
int pos = path.indexOf("/");
if (pos == -1) {
try{
String [] commands = new String [] {MSACCESS,path};
Process child = Runtime.getRuntime ().exec (commands);
}catch (IOException a){
System.err.println (a.getMessage ());
}
}
//if path has backslash, we know it's a website
else {
try{
String [] commands = new String [] {IE,path};
Process child = Runtime.getRuntime ().exec (commands);
}catch (IOException a){
System.err.println (a.getMessage ());
}
}
}
public ApplicationList ()
{
super ("Launcher");
setSize (150, 50);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//Store every application name in an array
//Make sure these names match the names in the HashMap EXACLTY!!
String AppNames[] = { "CNN", "db1", "db2", "db3", "db4",
"db5", "db6", "db7"};
File[] AppPaths = new File[8];
AppPaths[1] = new File("C:\\db1.mde");
AppPaths[2] = new File("C:\\db2.mde");
AppPaths[3] = new File("C:\\db3.mde");
AppPaths[4] = new File("C:\\db4.mde");
AppPaths[5] = new File("C:\\db5.mde");
AppPaths[6] = new File("C:\\db6.mde");
AppPaths[7] = new File("C:\\db7.mde");
String AppsLoaded[] = new String[8];
//add applications that everybody has; don't need to check for
AppsLoaded[0] = "CNN";
for(int i=1;i<AppNames.length;i++){ //don't check cnn, 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[20];
for (int i=0; i < AppsLoaded.length; i++) {
menulist[i] = new JMenuItem(AppsLoaded[i]);
menulist[i].addActionListener(this);
myMenu.add(menulist[i]);
}
myMenu.addSeparator ();
JMenuItem quititem = new JMenuItem("Exit");
myMenu.add(quititem);
quititem.addActionListener(this);
setJMenuBar (jmb);
setVisible (true);
}
}