1.11M Members

How can I have my program wait for input before continuing?

 
0
 

Hi everybody. I'm pretty new to java programming, just taking it as independent study my final year of high school, and I was looking for a little help. I was wondering if there was a way to have the program pause and wait for the user to tap any key before progressing. If you have any help for me it'd be wonderful.

Thanks in advance,
Dan

 
0
 

Simply implement the KeyboardListener. There is a lot of information on how to use this all over the internet.

 
0
 

Or, if thats a bit to bite off, you could look into JOptionPane. Try, say,

String someString = JOptionPane.showInputDialog("Enter someString");

This will prompt the user before entering someString in a text box. You'll need to import javax.swing.JOptionPane; or javax.swing.*; if you're going to use this. In order to make this work for "tapping any key", insert the line above wherever you wish a pause.

 
0
 

When you call this function it will generate a dialog.
It works the same as an JOptionPane. So the program will wait untill you close this dialog.
Only one future is that you can add your own container to it. So make a GUI in a class.

imports:

import java.awt.*;
import java.awt.event.*;
import java.swing.*;

on the point where you want to break for a message or a input call create a MyContainer en call the function.
So on that line you use those two lines

MyContainer mycontainer = new MyContainer();
showDialog(null,"Title of the dialog",mycontainer,Color.blue);

and put this function in your class.

public void showDialog(Component parentComponent, String title, MyContainer container, Color bg)
      throws HeadlessException {

    final JDialog dialog;
    int middleX = 400;
    int middleY = 400;
	
    Window window = new JFrame();          

    if (window instanceof JFrame) {
        dialog = new JDialog((JFrame)window, title, true);	
    } else if(window instanceof Frame) {
        dialog = new JDialog((Frame)window,title,true); 
    } else {
        dialog = new JDialog((Dialog)window, title, true);
    }

    dialog.setResizable(false);
    dialog.setLocationRelativeTo(parentComponent); //this function will set the dialog in the middle of the parent component
      
    dialog.getContentPane().add(container); //here you place the container in the dialog
    invulling.setDialog(dialog); //this will set a reference so you can close the dialog in your container class
    dialog.getContentPane().setBackground(bg); //here you set the background color, you can delete thisone if you like gray
    dialog.setSize(container.getPreferredSize()); //the dialog will resize to the size of your container, look at MyContainer for the size
    Dimension d = container.getPreferredSize();
    dialog.setLocation(middleX-(d.width/2),middleY-(d.height/2)); //if you have your own middle point use this function, otherwise delete this line

    dialog.show(); //and show the dialog
  }

and put this class as an innerclass in your code

Class MyContainer extends Container implements ActionListener{

 private JDialog dialog;
 private JButton ok;

 public MyContainer()
 {
   this.setSize(200,200);  //here you can set the size of the container and dialog
   
   ok = new JButton("OK");
   add(ok);
   ok.addActionListener(this);
 }

 public void setDialog(JDialog dialog)
 {
   this.dialog = dialog;
 }

 public void actionPerformed(ActionEvent e)
 {
   if(e.getSource() == ok)
   {
 	dialog.dispose(); //This will exit the dialog and the program will go on
   }
 }
}
 
0
 

All you need to do is to setModal(true). I'm dissapointed with the other lengthy responses.

 
0
 

Pretty cool response guys. But how do we assume that its necesarily a GUI program. Well if you are taking input from the command prompt I would suggest following

//get String or simply enter
public static String getStringFromUser(String msg)
{
String str="";
try
{
System.out.print(msg);
str = new BufferedReader(new InputStreamReader(System.in)).readLine();
}
catch (IOException e)
{
// TODO do exception handling
}
return str ;
}
 
0
 

B33FALO, is ur program GUI or in console?

 
0
 

I don't know how you could continue most programs without user input.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article