• Post Reply Bookmark Topic Watch Topic
  • New Topic

need a method to wait for input from another method and completly stumped  RSS feed

 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok im writting a program to help my DnD group and to learn a bit more java (prevously i have only delt with moddding in java for minecraft)

im using windowsBuilder (im told this is sometimes called swing?) to make the gui, pretty nice plugin to eclipse there.

But here lays my issue,
I have a text field for user input, a label to tell a player what he needs to do and a button to accept that input, (the input will then be placed into a variable, that all works)
I have another button that starts the char generation process, now as this method works i need it to go so far and then wait for the input button to be pressed. (this is really hard to explain)

so
the player hits the bottom begin button(we wall call this startBtn) and this starts our main method (we`ll call it main but it is in fact in the buttons select action itself)
main calls another class to make an array of all the "classes"(Dnd classes not java ones, ie wizard,claric,etc) from a txt file *working*
main changes the label to read "roll a d20 enter the result or type 0 to get random" *working*
main waits for enterBtn to be pressed ****this is where i cant figure out what to do!?****
main converts the string from the text field to an int *not done this yet but should be easy with a block of if textField == "1" then int textNumber = 1, or they may be a better way...havent looked at this yet as not there yet lol
main looks to see if a 0 is entered and if so randomly generates a number between 1 and 20 *not there yet but i know how to do that
main puts the result into a variable *again no probs
main then changes the label to read ""now roll a d10 enter the result or type 0 to get random"
main waits for enterBtn to be pressed *** just to show i need to wait for the button several times in this method
.....


i tried a CountDownLatch, it seemed perfect on paper but all that happened is the whole program frooze up not responding
i thought maybe i could use a loop inside main waiting for buttonPressed to be true and after it is carrying on setting buttonPressed to false for the next loop (and having buttonPressed set to true inside the enterBtn) but in my searches im told this is very bad to do...seems the only possibility to me at mo.

any help greatfully recieved
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi David,

one possible way, is to introduce a variable that monitors the state of your program.
So, when you have created some characters, after which you must wait for the user to input,
set the state to "waiting for user input".
Whatever happens next, you inspect the state to determine how to proceed.
You could also show that state in some JLabel, to indicate what the user is supposed to do.

Or you could pop up a modal dialog, in which the user must type some value before
being able to continue.

Greetings,
Piet
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Eastwick wrote:
i tried a CountDownLatch, it seemed perfect on paper but all that happened is the whole program frooze up not responding
i thought maybe i could use a loop inside main waiting for buttonPressed to be true and after it is carrying on setting buttonPressed to false for the next loop (and having buttonPressed set to true inside the enterBtn) but in my searches im told this is very bad to do...seems the only possibility to me at mo.


Swing is single threaded. Or to be exact, the dispatching of events is single threaded. You are not allowed to block on anything when processing events. Doing so will prevent the event dispatching thread from processing events -- and hence, the GUI will be non-responsive.

If you want to do waiting/blocking, or anything that takes time to run, you need to do the processing in a different thread.

Henry
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi David,

one possible way, is to introduce a variable that monitors the state of your program.
So, when you have created some characters, after which you must wait for the user to input,
set the state to "waiting for user input".
Whatever happens next, you inspect the state to determine how to proceed.
You could also show that state in some JLabel, to indicate what the user is supposed to do.

Thing is i dont understand how to do that, beyond of course having a small infinite loop that can only be broken out off when a variable = "you can can carry on" which i was told is a bad thing to do, though i have read som many forum posts on so many sites its possible i have got confused

so something like
public boolean doIWait = true

then in the method i need to wait
while (doIWait){Thread.sleep(500);}
doIWait = true

then in the method im waiting for input to go into i simply do
doIWait = false

(EDIT ...FAILED, became unresponsive again!)

Piet Souris wrote:
Or you could pop up a modal dialog, in which the user must type some value before
being able to continue.

Greetings,
Piet


hmmm im not a big fan of extra windows poping up but this may be the way to go (though i need to learn these ofcourse at some point)


It may be that i was brought up with BASIC so my mind sees programs as a continous flow, or even a flowchart so object orientated takes a bit of getting used to, i cant help thinking this is possibly a basic thing that every coder learns day one and i just dont know it lol
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
David Eastwick wrote:
i tried a CountDownLatch, it seemed perfect on paper but all that happened is the whole program frooze up not responding
i thought maybe i could use a loop inside main waiting for buttonPressed to be true and after it is carrying on setting buttonPressed to false for the next loop (and having buttonPressed set to true inside the enterBtn) but in my searches im told this is very bad to do...seems the only possibility to me at mo.


Swing is single threaded. Or to be exact, the dispatching of events is single threaded. You are not allowed to block on anything when processing events. Doing so will prevent the event dispatching thread from processing events -- and hence, the GUI will be non-responsive.

If you want to do waiting/blocking, or anything that takes time to run, you need to do the processing in a different thread.

Henry


i thought each method was its own thread? oh god, how do i set a method to be in a different thread?
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried moving the buttons functions into another class (which was a royal pain as variables seem to lose thier value between classes! but fixed that by making them static) however the program becomes unresponsive again at the same spot, i thought window builder was too good to be true!

ok so im officialy stuck, i do not understand where i go from here the task i need it to do seems so simple, but it just wont work intuitively, i had a look into threads and well i dont understand them would be an understatement


EDIT
ok i got around it by using the modal idea mentioned above,

String incomingDialog = JOptionPane.showInputDialog("enter a number");
diceRoll = Integer.parseInt(incomingDialog);
System.out.println("f1 = "+diceRoll);

the program does now wait for input...its not how i had planed it but working unplanned method is better then not working planed method
 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David, you could make an editor of sorts. Create a class CharacterSheet that holds all the information about a character, and has setters and getters for this information.

Design a JPanel form for each section of the character sheet, information that is only dependent on previous sections. For instance, Basic Info, Classes, Stats, Skills, etc. Now you can design a JFrame with a CardLayout, that you fill with one instance of each section. With Back and Next buttons you can then move to the next or the previous section, as long as the previous section was filled in completely.

This all may seem challenging at first, but if you get a hang of it you will learn a lot about event dispatching and model-view-controllers.

editor.png
[Thumbnail for editor.png]
 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:David, you could make an editor of sorts. Create a class CharacterSheet that holds all the information about a character, and has setters and getters for this information.

Design a JPanel form for each section of the character sheet, information that is only dependent on previous sections. For instance, Basic Info, Classes, Stats, Skills, etc. Now you can design a JFrame with a CardLayout, that you fill with one instance of each section. With Back and Next buttons you can then move to the next or the previous section, as long as the previous section was filled in completely.

This all may seem challenging at first, but if you get a hang of it you will learn a lot about event dispatching and model-view-controllers.



that does look cool, but most of the input from the user will be in the main method as they roll dice between tables, this seems more geared as a nice way to use the char sheet after, i confess as i read that i went a little crosseyed but i think i may look at that in detail afterwards.... i guess im going the get it working first then pretty it up approach lol.

the links to the java site are handy but i find that site to be more geared at people who are already java experts or maybe im just getting too old to easierly learn some stuff (im 35 years young) but they are appreciated thank you
 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I misunderstood the question. Can you explain more clearly what exactly it is you want the user to input, when you want the user to do it, and where you want to do it? I get the feeling that you want the user to do it in the console, but as has been proposed earlier, it might be much easier with modal dialogs.

Note that your use of the word "main" is confusing. In a Swing application, you don't use a "main" process. You let the Event Dispatch Thread react to actions the user performs. So what you need to do after the user presses the start button is to have the start button's event handler do everything you want to do until the next input is required.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Eastwick wrote: . . . how do i set a method to be in a different thread?
You don't.

Not in Swing, anyway; you can get strange errors like that. Look up in the Java Tutorials about Concurrency. You can start a parallel taks which does not affect your GUI with SwingWorker.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I give some code below, to show you how a modal dialog can be used.
The code itself is not important, it is just to show you how a modal input dialog
looks like.

Copy the code into whatever editor you use. Whenever you feel like,
press the inputmessage button. Then a modal dialog pops up,
asking to input some value. If you do that, and click the OK button,
you will see the inputted value in the console. Press some other button, and you will
see 'null' in the console.

The animation goes on, whether this messagedialog is open or not.

Is that something you might want to use?

Greetz,
Piet

 
David Eastwick
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Then I misunderstood the question. Can you explain more clearly what exactly it is you want the user to input, when you want the user to do it, and where you want to do it? I get the feeling that you want the user to do it in the console, but as has been proposed earlier, it might be much easier with modal dialogs.

Note that your use of the word "main" is confusing. In a Swing application, you don't use a "main" process. You let the Event Dispatch Thread react to actions the user performs. So what you need to do after the user presses the start button is to have the start button's event handler do everything you want to do until the next input is required.


As i say im more use to BASIC where its all one program ah good old gosubs where for art thou.

Im going to go the route of dialogs, its not my prefered vision of it but it works .

my plan was that it would all use the same window but that seems quite impossible....right i better get on with it

 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not impossible, you just need a button to confirm input. Here's a framework:
In the doStuff() method you can do whatever you want, depending on the value of step, print messages, and then simply enable the confirm button and return when you need more info. You can update step to tell a future invocation what needs to happen next.
input.png
[Thumbnail for input.png]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!