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!

code efficiency

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
I'm looking to store the application paths only once, instead of twice as I have currently have them. Is there a way to convert the file path over to a string for use in the HashMap or something along those lines? 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][b]
    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");
[/b]
    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];
[b]
        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");
        [/b]
        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);
    }
}
 
File f = new File("bla");
String path = f.getName();

--------------------------------------------------
Free Database Connection Pooling Software
 
thanks sedj, it was actually getPath() that I needed, but you pointed me in the right direction!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top