0

Although I am a beginner in java, I am trying to plot some data I have using JMathPlot. Every thing went well, but the script didn't terminate after I closed the window of the plot.

Could any body tells me how to force the script to terminate when I close the window of the plot.

I use a debian machine(32-bit), IDE = eclipse, java-version = 1,6 and here is the code I'm using for plotting:

    Plot2DPanel plot = new Plot2DPanel();
    plot.addLinePlot("test", x, y);
    JFrame frame = new JFrame("A test panel");
    frame.setContentPane(plot);
    frame.setVisible(true);

Thank you in advance

Taktech
  • 455
  • 1
  • 8
  • 18

2 Answers2

1

You termainate the process when closing the frame, not the panel.

This this:

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

Also see JFrame Exit on close Java

Community
  • 1
  • 1
rob
  • 1,286
  • 11
  • 12
0

Sure!

frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0)
            }
        });

What that does is, waits until the close button is pressed, then runs System.exit(0) which simply terminates your program.

Note: I wrote this on iPhone, so watch for syntax errors :)

Dean Leitersdorf
  • 1,303
  • 1
  • 11
  • 22
  • Yes, in this case you want to use that command. However, it's also good you know this, as in the future you might want to do other stuff when frame closes, not just termination. – Dean Leitersdorf Jul 03 '14 at 00:23