1

This is for an assignment created in BlueJ and submitted as a zip file containing the BlueJ package.

In the package are several independent console programs. I am trying to create another "control panel" program - a gui with radio buttons to launch each program.

Here are 2 of the listener classes I have tried:

private class RadioButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == arraySearchButton)
        {
            new ArraySearch();
        }//end if
            else if(e.getSource() == workerDemoButton)
            {
                new WorkerDemo();
            }//end else if
    }//end actionPerformed
}//end class RadioButtonListener

private class RunButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(arraySearchButton.isSelected())
        {
            new ArraySearch();
        }//end if
            else if(workerDemoButton.isSelected())
            {
                new WorkerDemo();
            }//end else if
    }//end actionPerformed
}//end class RunButtonListener

Thanks in advance!

2
  • 1
    So what's the problem/question? E.g. what happens (or doesn't happen)? How do you know it should be invoked? What is in WorkerDemo? Commented Apr 17, 2012 at 21:05
  • @pst The problem is that the console window never launches. Technically the homework is complete, this is extra. Commented Apr 17, 2012 at 21:14

1 Answer 1

1

Assuming you are trying to launch .EXE console applications, here's some code that could help you. See below for explanation.

import java.io.*;


public class Main {

       public static void main(String args[]) {

            try {
                Runtime rt = Runtime.getRuntime();
                //Process pr = rt.exec("cmd /c dir");
                Process pr = rt.exec("c:\\helloworld.exe");

                BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

                String line=null;

                while((line=input.readLine()) != null) {
                    System.out.println(line);
                }

                int exitVal = pr.waitFor();
                System.out.println("Exited with error code "+exitVal);

            } catch(Exception e) {
                System.out.println(e.toString());
                e.printStackTrace();
            }
        }
}

First you need an handle on current running java application and to do so you create an Runtime Object and use Runtime.getRuntime(). You can then declare a new process and use the exec call to execute the proper application.

The bufferReader will help print the output of the generated process and print it in the java console.

Finally, pr.waitFor() will force the current thread to wait for the process pr to terminate before going further. exitVal contains the error code if any (0 means no error).

Hope this helps.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the extensive code, very informative. The console applications, however, are actually other classes in the same BlueJ package as the control panel
Then why can't you simply create an instance of that class and call the proper functions?
I thought that's what the above code did; did I miss something?
The above code lets you create a new process specifying the path to an executable file (.exe). If you have multiple classes in your package you can simply instantiate (aka create an object) of that class and then use it in your control panel. I don't know what's your level of expertise in Java and I'm unsure right now what you are trying to do. Perhaps edit your original post with some more details. Perhaps even a screenshot of your project and more details on what you are really trying to achieve would help.
Sorry, by "above code" I meant the code that I posted as part of the question. I am using new Classname(); in the action listeners, is that not enough?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.