How to halt a java program until a method is called successfuly? | SoloLearn: Learn to code for FREE!


How to halt a java program until a method is called successfuly?

---BACKGROUND-- I'm doing a big project at the time - it can deal with history logs of a game. In that project I created a method which returns logs within a given time - and it works very well. I decided to create a UI for that program. Also I added a button which popups a JPanel to select date ranges - there's the problem start. --PROBLEM-- The popup pops out successfuly upon a butn click and user can select the date and submit. But I have methods which evaluates after that. What the problem is that the data from the popup is not submitted properly to the main program. I just debugged to find out what was happening. Then I found that the following methods evaluates before the popup pops out. --QUESTION-- My question is how to halt the program until the user submits the dates and then again start the program. I m looking for a way like JS promises.

6/12/2019 1:36:43 PM


4 Answers

New Answer


Sεηιrυ ραsαη Not your answer but take a look at SwingUtilities invokeAndWait function. And probably you need to create a new thread (as you are already on Event dispatch thread) on button click to show JPanel. Note: I have not done swing programming since quite long so it is as good as i don't know anything, but few things you do remember 😉


Belal Al-Qudah I think I have forgotten to mention this. The code below the popup invokes before the picker pops out


after you submit you can wait() or sleep() . look into those functions . use them with while statement like while( finishTarnsmit()==false) (new thread).sleep(100)


Are you using the Model-View-Controller structure? You should be able to add a line in after the popup in order to wait for the input from the popup, and then evaluate it or continue. Simplet form would be having a boolean "inputReceived" initialized to false, and a while loop after the popout for while received==false {sleep (x)} Once your popout finishes, have it set received to true so that next time it evaluates it continues. This is not the best approach, however it is the simplest.