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

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

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.

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
   }
 }
}

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

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 ;
}

B33FALO, is ur program GUI or in console?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.