tags:

views:

121

answers:

3

I have written a java program and I am running it through command line like "java MyProgram"

Now I want to have a GUI that have a start, pause and stop button. How to start that java program by clicking on start button. How to pause it and how to stop it?

A: 

First you need to write your "Forms"...

This will be a helpful resource to a beginner.

Basics

Chathuranga Chandrasekara
I think that addresses a different problem...
fortran
+4  A: 

I assume your program is something like:

public class MyProgram {

  public void doSomething() {
    // ... does something ...
  }

  public static void main(String[] args) {
    new MyProgram().doSomething();
  }
}

I recommend reading the Swing Tutorial, but a simple GUI to launch your program could be something like:

public class MyProgramLauncher {
  public static void main(String[] args) {
    final MyProgram myProgram = new MyProgram();

    JFrame frame = new JFrame("My Program");
    JComponent cp = frame.getContentPane();
    cp.add(new JButton(new AbstractAction("Start") {
      public void actionPerformed(ActionEvent e) {
        myProgram.doSomething();
      }
    }));
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}

If your class has a pause() function, you could add a similar JButton to call that function, but you'd have to write/implement the function.

You would, however, have to launch this with java MyProgramLauncher, which isn't very exciting. Still, that will get you a basic GUI in which you can experiment with starting, pausing, etc.

To turn your program into something you can double-click on, you'll need to create a JAR file. This is basically a special ZIP file that includes all the classes in your application, plus a manifest.xml file that describes those classes and (for launchable JAR files) identifies the "main class" whose main() method should be called when the JAR file is launched.

To turn that JAR file into a more or less self-contained deployable application is a bigger pain and there are a whole lot of options. The Deployment Tutorial might be a place to start.

David Moles
+1  A: 

Basically you need a native launcher, but I cannot figure out what do you mean exactly by "pause"... Sending the process to sleep?

I think that should be very easy to implement with a shellscript using the xdialog command in a Unix like system.

You'll need to implement a state machine:

  • State: "Stopped"
    • Start: execute java YourProgram and store the PID in a variable. Change state to "Started"
    • Pause: do nothing/disabled
    • Stop: do nothing/disabled
  • State: "Started"
    • Start: do nothing/disabled
    • Pause: send the STOP signal (like ctr+z) to the process. Change state to "Paused"
    • Stop: send the INT signal (like ctr+c) to the process. Change state to "Stopped"
  • State "Paused"
    • Start: do nothing/disabled
    • Pause: send the CONT signal (like doing fg) to the process. Change start to "Started"
    • Stop: send the INT signal (like ctr+c) to the process. Change state to "Stopped"

With this, you can loop in the script and react to the buttons. Look the reference for kill and dialog or xdialog for more details on the implementation.

fortran
A native launcher isn't necessary. A executable .jar works just fine and doesn't artificially restrict the platform compatibility.
Joachim Sauer
but it doesn't have buttons, just starts the application... he needs something graphical -to launch- (not after it's already launched) his application, right?
fortran