Vagelism
Vagelism

Reputation: 601

Java. How to wait?

I call a class that creates a jframe and waits from user to input some values. The problem that I experience is that I need to wait these values before to continue. So the code is something simple like this

Jframe frame= new jframe(); //here I want the program to show the frame and then wait till   it will be disposed
// I want a pause here 
System.out.println(frame.getvalue);

Till now the only I could do is to froze the frame before can even appear totally. Any help? Please keep it simple since I am new to Java. THANK YOU!

Upvotes: 1

Views: 3046

Answers (5)

Kylar
Kylar

Reputation: 9334

This will cause the current thread to wait 5 seconds:

try {
    Thread.currentThread().wait(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

Upvotes: -3

hat
hat

Reputation: 191

What you're probably looking for is JOptionPane. This is a blocking routine that returns only after the user has entered some value, like so:

public class test
{
  public static void main ( String args[] )
  {
      String input = JOptionPane.showInputDialog(null, "Thing: ",
                    "Enter Stuff", JOptionPane.OK_CANCEL_OPTION);

      System.out.println ( "won't reach until got input");
      System.out.println ( "My value: " + input );
  }
}

The great thing about it is you can add Components to it, so you aren't limited to a single input field, but it is still blocking. The following would add two JTextField's to the frame:

public class test
{
    public static void main ( String args[] )
    {
        JTextField input_box = new JTextField(7);
        JTextField input_box2 = new JTextField(7);

        JComponent[] inputs = new JComponent[] {
            new JLabel("Thing 1:"),
            input_box,
            new JLabel("Thing 2:"),
            input_box2 };

        int rval = JOptionPane.showConfirmDialog(null, inputs,
                    "Enter Stuff", JOptionPane.OK_CANCEL_OPTION);

        if ( rval == 0)
        {
           System.out.printf ("%s and %s!", input_box.getText(),
                                         input_box2.getText());
        }
    }
}

Upvotes: 5

engma
engma

Reputation: 1959

well as you know swing components are not thread safe though you can use SwingWorker to make the waiting in background,

It uses the thread way but it creates a new thread for the waiting ,long term operations in general, instead of pausing the event dispatch thread so the user can interact with the rest of the application or the rest of the application can continue to work while the waiting goes on.

ofcourse you have to define a way for it to stop the waiting.

check out its documentation here http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html

Upvotes: 0

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

Instead of using a JFrame, consider using a JDialog with modality set to true.

When it comes time to add an 'OK' button or something like that, check out JRootPane.setDefaultButton()

Upvotes: 4

Andriy Budzinskyy
Andriy Budzinskyy

Reputation: 2001

I think you should use JDialog instead of JFrame. Please follow this example

Upvotes: 10

Related Questions