how do I keep a persistent prompt in JTextArea and allow user input
Hi,
I'm trying to simulate a unix command prompt and accept input at the same time. For example, I'd like to show the prompt "localhost#>" and program it to answer to commands like ifconifig -a and show the results as if it were a unix host. After the user types enter I would like to show the prompt again along with the results of the typed commands. I think I've gone through every single JTextArea tutorial Sun has but is has not helped. How can I do this please?
Re: how do I keep a persistent prompt in JTextArea and allow user input
import java.util.Scanner;
declare the following in your constructor:
Code :
source = new Scanner(System.in);
declare the variable:
us this method for your command prompt:
Code :
public void getCommands()
{
while(true){
System.out.println("=========================");
System.out.println("======COMMANDS===========");
System.out.println("=========================");
System.out.println("1. Command");
System.out.println("2. Command");
System.out.println("3. Command);
System.out.println("4. exit");
System.out.println(" ");
System.out.print("Input: ");
int s = source.nextInt();
if ( s == 1 ){
//do something
} else if (s == 2){
//do something
} else if (s == 3){
//do something
} else if (s == 4){
System.out.println("Exit selected, have a nice day!");
System.exit(-1);
}
}
}
I think this is what you are trying to do. This will create a command line program and will be controlled in terminal. You could also add and else if for command not found aswell.
Re: how do I keep a persistent prompt in JTextArea and allow user input
i think this is kind of what ur looking for , though its a jtextfield i think most of the essential stuff can transfer over
Code :
add(getJTextField0(), //the constrains and stuff);
//the method to get the text field added
public JTextField getJTextField0() {
if (jTextField0 == null) {
jTextField0 = new JTextField();
jTextField0.setText("Type Here");
jTextField0.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent event) {
jTextField0KeyKeyReleased(event);
}
});
}
return jTextField0;
}
// the key listener and text field input collector
public void jTextField0KeyKeyReleased(KeyEvent event) {
if (event.getKeyCode() == /* the enter key's code*/ ){
String input = jTextField0.getText();
// do what ever the command would do
}
}