Results 1 to 8 of 8
  1. #1
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default How to make a make a Console program?

    So currently I am trying to use a JTextarea to display text and I then prompt users to type in certain words. I have a few problems that I have encountered. Firstly when I use setText and then if I say setText again after that it erases the previous text that was set. Ideally I want to set text once per line in my whenever a certain task is fulfilled. For example if the text says enter y/n, I want the program to go to the next line once I say y or n. Another problem of course is when I setEditable to true I can edit the whole textArea. I just want to be able to edit the line which I am currently at. Is there anything that I can use that mimics the ACM console program, or even the console down below in most IDE's where everything appears to be done line by line? Any examples would be great, I am not sure what to build to in order to fulfill my goals.

    Also here is an example of what I am trying to do, along with comments:

    Java Code:
     console.setText("Math is good!");
      console.setText("What is 2+2?: ");  //using getText gets the whole line, I want 
    //everything checked after the colon.  That is where the input will be
    if(line==4){
    console.setText("That is correct");
    }
    else {
    console.setText("That is incorrect");
    }
    //previous text lines are overriden with new ones. I don't want that, nor do I want to be able to edit the 
    //whole JtextArea
    A JtextArea is just not getting the job done. Any thoughts?
    Last edited by Snake006; 11-09-2014 at 03:00 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to make a make a Console program?

    if I say setText again after that it erases the previous text
    The JTextArea has other methods that will append the data to what is currently there. Read the API doc.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to make a make a Console program?

    Quote Originally Posted by Snake006 View Post
    Java Code:
     console.setText("Math is good!");
      console.setText("What is 2+2?: ");  //using getText gets the whole line, I want 
    //everything checked after the colon.  That is where the input will be
    if(line==4){
    console.setText("That is correct");
    }
    else {
    console.setText("That is incorrect");
    }
    //previous text lines are overriden with new ones. I don't want that, nor do I want to be able to edit the 
    //whole JtextArea
    A JtextArea is just not getting the job done. Any thoughts?
    Yes, this problem is too big for you now; you have to implement two streams, one for input and one for output; to keep things easier, make them character streams. Also, as it is, the JTextArea is too powerful for the job (e.g. why should the input follow that colon in your example if the user can press cursor movement keys or even click the mouse and set the text cursor position?) InputMaps can handle that inconvenience if you know what you're doing, but for now this problem is too complicated for you; I suggest you use a separate JTextField for the input and a (non-editable) JTextArea for the output; keeping them in sync is complicated enough.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: How to make a make a Console program?

    If this isn't homework, you may be able to use camickr's Message Console Java Tips Weblog

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: How to make a make a Console program?

    ok that helps. Also what about erasing all old text. Say if I wanted to clear all text after 5 or 6 lines, how could I do that?

  6. #6
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: How to make a make a Console program?

    Actually I figured that part out. Now I am trying to use an action performed method that calls a method when the button is pressed. How I cannot exit the method once it is called, or rather I don't know how to end the action performed once I have pressed a button. Any thoughts

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: How to make a make a Console program?

    Have you discovered the Oracle tutorials? https://docs.oracle.com/javase/tutor...ts/button.html

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to make a make a Console program?

    how to end the action performed
    Can you explain what that means?
    When execution exits a method, that ends the execution of the method. The return statement will cause execution to exit a method.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: 01-30-2014, 02:56 PM
  2. Replies: 1
    Last Post: 04-16-2013, 10:56 AM
  3. How to make a Java Console Program save user made changes?
    By Chad.Curtiss in forum New To Java
    Replies: 3
    Last Post: 07-14-2012, 08:58 PM
  4. How to make a "real" console program?
    By Pojahn_M in forum New To Java
    Replies: 3
    Last Post: 12-26-2011, 02:15 AM
  5. Replies: 4
    Last Post: 07-23-2011, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •