0

I have two buttons on my Netbeans GUI form. One button is starting button, and the other is stopping button. When I press the start button, my code runs good, but when I try to stop then the stop button seems disabled. I cannot press the stop button.

My code starts from the GUI, in which this is my code for starting button

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
    live p=new live(); //this calls the method of my application
    p.livecapture();
}

code for stop button

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
    mm=2;
}

The following while loop is present in my livecapture() method

while(NewJFrame.mm!=2) {
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Xara
  • 8,748
  • 16
  • 52
  • 82

2 Answers2

4

Suggestions:

  • Make sure your capture loop is off of the event thread.
  • If on the event thread either move it off or use a Swing Timer.
  • Give the capture class a state variable that will stop its loop and a setter method that other classes the ability to tell it to stop.
  • In it's loop, have it check its state variable and if set, stop the loop.
  • Please read up on and follow Swing naming conventions. Your code does not adhere to them making it hard for outsiders (us!) to read and understand it.

For more help, tell us more about your live class, what it does, what it does when "capturing".

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • My live class captures packets coming on an interface. – Xara May 16 '12 at 15:46
  • @Zara: So does it do this off of the event thread? Look at Jamie's excellent suggestion on this (1+ to him for this). – Hovercraft Full Of Eels May 16 '12 at 15:49
  • I have added my full live class code.Actually i have used swing worker previously but my results used to come fast and sometimes wrong as well thats why i am not using swing worker. – Xara May 16 '12 at 15:54
  • @Zara: go back and use a SwingWorker and fix the bugs that you had. Do not do this sort of coding on the Swing Event thread, the EDT, unless you want your program to become completely unresponsive (as you are now experiencing). – Hovercraft Full Of Eels May 16 '12 at 15:58
  • 1
    +1 on all counts; there's a related `javax.swing.Timer` example [here](http://stackoverflow.com/a/2576909/230513). – trashgod May 16 '12 at 16:03
4

I'm not sure on your situation, but it sounds like you're executing your code from the Event Dispatch Thread (EDT). This is the thread in your program that is responsible for listening to your GUI-generated events (among other things). If your EDT is busy processing the code in the listener for the start button, then it will be blind to any other button presses that happen.

To prevent this, you should make your program multi-threaded. This is a huge topic in any programming language, but here is a simple example of one of the many ways it could be done in Java, using a Swing Worker:

class MyWorker extends SwingWorker<Void, Void> {
    public Void doInBackground() {
        // The code you want to run
        return Void;
    }
}

MyWorker thread = new MyWorker();

startButton.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
        thread.execute();
    }
} );

endButton.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
        thread.cancel();
    }
} );
Jamie
  • 3,890
  • 3
  • 26
  • 35