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!

How to access arrays in multiple locations

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
My code is working, however I'm trying to be efficient and I would like to use "AppNames" & "AppPaths" to execute programs instead of how I have it hardcoded now with the CNN site address. How can my code "read" from the arrays in a different area? I've tried using the private/public/public void method for the array, but I couldn't get it to work. Is it possible to search an array for an item such as "App1" and return the corresponding placeholder then I could use that to return the correct AppPath? thanks in advance!

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

public class ApplicationList4 extends JFrame implements ActionListener {
    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 ApplicationList4();
    }
    public void actionPerformed(ActionEvent e) {
    JMenuItem source = (JMenuItem)(e.getSource());
    System.out.println(source.getText() +" clicked");
    String cmd = source.getText();
    if (cmd.equals ("CNN")){ [b]//AppNames[] should be read here[/b]
try{
    String [] commands = new String [] {IE,"[URL unfurl="true"]http://www.cnn.com"};[/URL] [b]//AppPaths[] should be read here[/b]
    Process child = Runtime.getRuntime ().exec (commands);
} catch (IOException a){
    System.err.println (a.getMessage ());
        }
    }
}

    public ApplicationList4 ()
    {
        super ("Launcher");
        setSize (150, 50);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        
        //Store every application name in an array
        String AppNames[] = { "App1", "App2", "App3", "App4", "App5",
        "App6", "App7", "App8"};
        
        //Store every application path in an array
        File[] AppPaths = new File[10];
        
        AppPaths[0] = new File("[URL unfurl="true"]http://www.cnn.com");[/URL]
        AppPaths[1] = new File("C:\\App2.mde");
        AppPaths[2] = new File("C:\\App3.mde");
        AppPaths[3] = new File("C:\\App4.mdb");
        AppPaths[4] = new File("C:\\App5.mde");
        AppPaths[5] = new File("C:\\App6.mde");
        AppPaths[6] = new File("C:\\App7.mde");
        AppPaths[7] = new File("C:\\App8.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[20];
        
        //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 ();
        myMenu.add("Exit");

        setJMenuBar (jmb);
        setVisible (true);
        
    }
}
 
Use a Hashtable and read about it (java.util.HashMap) in the docs:

Code:
HashMap cmds = new HashMap ();
cmds.put ("App1", "C:\\App1.mde");
cmds.put ("App2", "C:\\App2.mde");
// ...
cmds.put ("App9", "C:\\App9.mde");


//...
String name = "App7";
String path = cmds.get (name);

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top