1

My title is somewhat cryptic but I couldn't come up with a clear one.

First, two code snippets to establish a point of reference (hopefully I have no typos):

Input with Scanner

'''
Scanner sc = new Scanner(System.in);
for (int i=0; i<10; ++ i)  {
  System.out.print("Please input a value:");
  String answer = sc.next();
  // do something with this string
}  
...

Input with JOptionPane:

...
for (int i=0; i<10; ++ i) {
   String answer = JOptionPane.showInputDialog("Please input a value");
   // do something with this string
{

So, in the above samples we're asking a user to enter a value a fixed number of times. How can I implement the same kind of functionality in a Swing application?

I have no problem creating a JFrame with JPanel (as its content pane) and adding JLabel (with prompt) and JTextField to this panel. I can also create ActionListener for the text field which ActionPerformed method to retrieve the value and process it. String processing is not a long-running task so I do not believe I will need a separate worker thread.

Since we can't really force user to do anything, I plan to use javax.swing.Timer to ensure a timely response.

What I do not understand is how to implement the loop or any other form of control to ensure that a user enters (and the program retrieves) the value the exact number of times. How do I inject such logic into an event-driven system?

Once I set-up the GUI part and submit its instance to be invoked on EDT I seem to be relinquishing all control.

Do I initially submit my text field with setEditable set to false and then create a loop that will invokeAndWait a Runnable to enable the edit (and disable it back in the ActionPerformed)?

Please point me into the right direction.

3
  • Use a JOptionPane to hold the flow, till user enters something to proceed with. Is there a problem with the second approach, that you defined? Please elaborate on that. Commented Feb 27, 2014 at 3:00
  • Do you not want to use a dialog for some reason? You can put basically anything inside one and modality is hard to pass up for certain things. With the counting--bottom line is you probably have to write some kind of object for this. Commented Feb 27, 2014 at 3:09
  • I do not want to use JOptionPane and want to use JTextField. I'm creating quite a complex GUI screen for a math game. Commented Feb 27, 2014 at 3:18

1 Answer 1

5

Well it depends on how you want to achieve it...

You could...

Provide the required number of fields (10 in your example) and a JButton, so that until all the fields are filled out, clicking the button will simply provide the user with a message and re-focus the invalid field...

You could...

Provide the user with a single field (and label) and button. Until they fill out the field, pressing the button prompts them and re-focuses the field.

When the user fills out the required information and clicks the button, you increment a counter, reset the field and carry on until your counter reaches it's limit...

You could...

Use a JTable which has only one column and five rows...this is simplified (depending on your perspective) solution to the first solution...

Sign up to request clarification or add additional context in comments.

4 Comments

My requirement is that entry is completed when a user presses Enter or the time expires (Timer fires an event) whatever comes first. No additional control buttons.
Okay, second option, no button and action listener on field...Hope your user knows how to move to the next "question" ... JOptionPane has buttons BTW ;)
Only one text field can be open for entry at a time and it should have the focus.
Yes, it seems that button-less 2nd option could work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.