Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade Newsgroups: comp.lang.java.programmer Subject: Re: Interrupt code while running (from Form) Date: Thu, 20 Oct 2016 10:33:39 +0100 Lines: 20 Message-ID: References: <6628a02c-9c5a-4cdc-8113-fcf13c972ea4@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net S6wd2QlxWgxA7h7SVrYPVg8W12GY/eq6L9MP9UJy38aeLeZwo7 Cancel-Lock: sha1:xpOop81Xz4FsucAoTcl//meDiyE= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 In-Reply-To: <6628a02c-9c5a-4cdc-8113-fcf13c972ea4@googlegroups.com> Xref: csiph.com comp.lang.java.programmer:35846 On 20/10/16 09:44, jkl wrote: > Hi > > Sorry for pretty basic question - I'm relearning. > > I'm learning Java again after several years and running Netbeans. I have a simple little form which I'm using to report the output of > prime numbers starting from an integer entered by the user, then the system counts up to another user defined number listing all the prime numbers in between. Pretty simple stuff. > > ... The trouble is, even running in Netbeans, the only way to interrupt it while running is to go outside the form and use the Stop button in Netbeans IDE. I have put a stop button on the form but however many times I click it while the code's running - it's being ignored. > > This is all OO stuff so I'm assuming there's no way within the body of the code I can check for the stop button being pressed. > > I'm missing something fundamental - but can't think what. I would guess that you are missing the fact that if you execute any long-running method on the EDT (Event Dispatch Thread) then the UI will not be able process any events. It's not about Java or OO, it's about understanding how Swing threads and events work. I recommend that you read the Java tutorial on Swing: http://docs.oracle.com/javase/tutorial/uiswing/index.html In particular, if you want to run background/concurrent tasks then it's vital that you read the chapter on Concurrency in Swing; http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html