crashing after actionperformed
I've the following problem.
I want to perform an action every 5 seconds.
If I click stop it has to stop.
If I click start it has to begin again.
But after clicking start it crashes always.
Does anyone know another solution?
I also tried it with treads but that gave other problems.
Here is my source (a little example of the crash in my app):
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
class test1
{
JFrame f;
JButton start, stop;
JPanel contentPane;
boolean going = true;
public static void main(String[] args)
{
new test1();
}
test1(){
f = new JFrame("the big test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(new Dimension(400, 300));
f.setVisible(true);
start = new JButton("Start");
stop = new JButton("Stop");
contentPane = (JPanel) f.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(start, BorderLayout.NORTH);
contentPane.add(stop, BorderLayout.SOUTH);
ActionListener al = new MyActionListener();
start.addActionListener(al);
stop.addActionListener(al);
go();
}
void go(){
while(going){
try{
Thread.sleep(5000);
}
catch (Exception e){System.err.println(e);}
new test2();
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == start){
going = true;
go();
}
if (e.getSource() == stop){
going = false;
}
}
}
}
class test2
{
test2(){
System.out.println("Just printing a line");
}
}

