Simple User-Input AKA a DOS application.

I've been doing some programming using the simple DOS commands, and reading keyboard input to read strings into variables.

I've got an Applet at the moment with a text area (several rows high), and a text field (single line), and I've got the text area reading a string variable into it, and the text field appending it's value to the variable on the event that you hit enter..

But the issue I'm having is that, unlike the DOS commands, I can't get the processing to stop and wait for an input, then continue...

If anyone could help out, it would be great, Thanks!

[595 byte] By [deanrathera] at [2007-10-3 8:30:37]
# 1
Are you using an ActionListener?
CaptainMorgan08a at 2007-7-15 3:37:39 > top of Java-index,Desktop,Core GUI APIs...
# 2
Yeah.
lord_spatulatora at 2007-7-15 3:37:39 > top of Java-index,Desktop,Core GUI APIs...
# 3
Im assuming your deanrather. Could you post your code?
CaptainMorgan08a at 2007-7-15 3:37:39 > top of Java-index,Desktop,Core GUI APIs...
# 4

Here's my code at the moment... the commented out lines in the input class are wherre im playing around at the moment...

Any help or references appreciated.

Thanks :)

// File: Console.java

package MyConsoleIO;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class Console extends JApplet

implements ActionListener{

private JTextField inputBox;

private JTextArea textBox;

private String gotString = "";

private Game game;

public void trace(String msg){System.out.println(msg);}

public void println(String msg){

textBox.append("\n"+msg);

trace("Tried to print: "+msg);

}

public void init() {

game = new Game();

try {

SwingUtilities.invokeAndWait(new Runnable() {

public void run() {

createGUI();

}

});

} catch (Exception e) {

System.err.println("createGUI didn't successfully complete");

}

}

private void createGUI() {

JPanel contentPane = new JPanel(new GridBagLayout());

contentPane.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createLineBorder(Color.BLACK),

BorderFactory.createEmptyBorder(10,10,5,5)));

setContentPane(contentPane);

GridBagConstraints c = new GridBagConstraints();

c.insets = new Insets(0,0,5,5);

//// Label

//JLabel receiverLabel = new JLabel(">",

//JLabel.TRAILING);

//add(receiverLabel, c);

// Text Field

inputBox = new JTextField("Type Something", 10);

c.fill = GridBagConstraints.HORIZONTAL;

c.weightx = 1.0;

add(inputBox, c);

inputBox.addActionListener(this);

// Button

JButton button = new JButton(">");

c.gridwidth = GridBagConstraints.REMAINDER; //end row

c.anchor = GridBagConstraints.LINE_START; //stick to the

//text field

c.fill = GridBagConstraints.NONE; //keep the button

//small

c.weightx = 0.0;

add(button, c);

button.addActionListener(this);

// Text Box

textBox = new JTextArea(30, 80);

textBox.setEditable(false);

c.anchor = GridBagConstraints.CENTER; //reset to the default

c.fill = GridBagConstraints.BOTH; //make this big

c.weighty = 1.0;

add(new JScrollPane(textBox), c);

}

public void actionPerformed(ActionEvent event) {

textBox.append("\n"+inputBox.getText());

gotString = inputBox.getText();

inputBox.setText("");

Input.inString = gotString;

}

public void start(){

textBox.append(Output.messagelist);

}

}

// File: Game.java

package MyConsoleIO;

public class Game{

public Game(){

Output.println("Game Started!");

String name = Input.getString("Please enter your name: ");

Output.println(name + ", ay... how odd.");

Output.println("GAME OVER");

}

}

// File: Input.java

package MyConsoleIO;

public class Input{

static public String inString = "";

public static String getString(String message){

Output.println(message);

//while(inString.length() == 0){

//// wait for some input...

//}

return inString;

}

}

// File: Output.java

package MyConsoleIO;

public class Output {

public static String messagelist = "";

public static void println(String line){

messagelist += "\n" + line;

}

}

lord_spatulatora at 2007-7-15 3:37:39 > top of Java-index,Desktop,Core GUI APIs...
# 5
And what exactly is going wrong?
CaptainMorgan08a at 2007-7-15 3:37:39 > top of Java-index,Desktop,Core GUI APIs...
# 6
I want it to print up"game started""what is your name?"then wait for some input before continuing.instead it just prints everything up all at once... then stops doing anything...
lord_spatulatora at 2007-7-15 3:37:39 > top of Java-index,Desktop,Core GUI APIs...
# 7

In your Game class, these two lines of code:

Output.println(name + ", ay... how odd.");

Output.println("GAME OVER");

These should be put in their own method. Then when the a name is inputed, you can call that method. Does that make sense?

CaptainMorgan08a at 2007-7-15 3:37:39 > top of Java-index,Desktop,Core GUI APIs...
# 8

Yeah, I know that would work, sorry, I forgot to mention.

Game.java is an example of the code for a dos game I made (which has extensive calls to getString(), and println() methods within an Input and Output, class.

If it's not possible i'll just have to go through and re-write all the code for my game, but I'd really rather not :p

So yeah.. if there were some way I could make getString in the Input class wait untill some uer input has been made, and return that, i should be about set!

Thanks if you can help!

lord_spatulatora at 2007-7-15 3:37:39 > top of Java-index,Desktop,Core GUI APIs...