0

I'm trying to create a frame in Java using NetBeans. It will display some data and at the same time get data from the user in a while loop.

Can I pause the while loop until a confirm button has been pressed? I found that someone may suggest sleep, but since I have to let the user enter the data, I can't predict how long they are going to take.

Glenn
  • 8,932
  • 2
  • 41
  • 54
  • why would you loop in the first place when you are still waiting for the user to press the button/ input data? – SomeJavaGuy Nov 23 '15 at 11:58
  • So by "in Netbeans" you probably mean you're using the Netbeans user interface designer to draw the window of your program? Netbeans is only an editor, you are still creating a Java program here and not a "netbeans program". – Gimby Nov 23 '15 at 12:03
  • Kevin Esche, that is because in different situation, I have different number of input from user. For example, in situation 1, the user need to enter 3 data. Whereas, in situation 2, 4 data to enter. That's why I use loop. – user5595027 Nov 24 '15 at 12:11
  • Gimby, Thanks for correcting me. – user5595027 Nov 24 '15 at 12:12

1 Answers1

3

The event driven model of a GUI breaks sequential logic code.

To keep the sequential logic, one could maintain a state, for instance an enum:

enum State {
    PROMPTING,
    ASKING_TWO_FIELDS,
    ASKING_THREE_FIELDS,
    CALCULATING,
    DONE
};

private State state = State.DONE;

(This explicit abstraction is often only needed for transition from sequential logic, or multiple states on the same forms.)

Then on button press and such change the state and disable/enable input components.

Events happen asynchrone, even possibly on different threads. One must write reactive code. And on preparing things wire together what should happen on a button press. This is very fragmentary.

One solution to this chaos is separating into Model-View-Control classes. Model classes contain the data, View are the GUI components, and one Controller, the application, is called from the views, sets the data, and controls the views.

Being called from the views is for swing done using listeners.

A recipe:

  • Make a main class being the Controller
  • A Model class for data
  • The top View, a GUI class, a JFrame (swing) or JavaFX scene
  • The controller keeps model and view as fields
  • The controller passes itself to the view
  • Map your sequential logic to an enum State, in the controller
  • Write transition function(s) switchState.
  • Instead of waiting, add ActionListeners to a butten (swing) / ButtonHandlers (JavaFX) that call i.a. switchState.
  • Best use method names expressing business logic: startTranslating (text dropped on application), manualTranslationDone (button "translated" pressed after manual editing).
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Note: In NetBeans, the `JFrame`s are already created with a `main class` structure, and, in my opinion, since GUI apps can and should be based around GUI input and output, it makes more sense to have the `JFrame` (GUI) as the main class (in the sense of starting and running the program). To maintain an MVC model, have the GUI events just bind to calls to methods in the `controller`, that receives instructions from the GUI and that is responsible for the organization and execution of `model`s and the processes within them, through the EDT (Event Dispatch Thread) or other "worker threads". – CosmicGiant Nov 24 '15 at 14:55
  • @TheLima I am a NetBeans user too, and can agree with that: a bit simpler to create the controller in the JFrame. Thanks for giving an extra perspective. – Joop Eggen Nov 24 '15 at 15:48