Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: how do I keep a persistent prompt in JTextArea and allow user input

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Thanked 0 Times in 0 Posts

    Default how do I keep a persistent prompt in JTextArea and allow user input

    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?

  2. #2
    Junior Member
    Join Date
    Dec 2009
    Thanked 0 Times in 0 Posts

    Default Re: how do I keep a persistent prompt in JTextArea and allow user input

    import java.util.Scanner;

    declare the following in your constructor:
    source = new Scanner(System.in);

    declare the variable:
    private Scanner source;

    us this method for your command prompt:
    public void getCommands()
    	        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!");

    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.
    Last edited by helloworld922; February 23rd, 2010 at 03:11 PM. Reason: please use [code] tags

  3. #3
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    the middle of the woods
    Thanked 1 Time in 1 Post

    Default 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

    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) {
    	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
    Last edited by wolfgar; February 23rd, 2010 at 12:18 PM. Reason: noticed it was area u needed and not field XP
    Programming: the art that fights back

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  3. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  4. a persistent error
    By iank in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 5th, 2009, 08:12 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM