Results 1 to 4 of 4
  1. #1
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Question Create Command Line Dynamically Updated Lines?

    I tried searching, but I'm likely using bad search terms! Please help!

    I'm trying to create a command line program that can dynamically update multiple lines. A good example of what I'm attempting to accomplish is the "top" program in linux... which is way more than what I'm doing but has multiple lines that are dynamically updated based on whatever data they need to contain.

    So far i've been able to figure out how to update text in a single line without creating a new line, but I'm at a loss for creating multiple lines and updating those without re-creating those lines each time...

    for example... dynamically updating a single line:

    Java Code:
    public static void main(String[] args) throws Exception{
        String text1 = "Old Text";
        String text2 = "New";
        System.out.print(text1 + "\r");
        Thread.sleep(600);
        System.out.print(text2);
    }
    but i'm needing to do this with 3 lines at the same time... would be nice to not have to spam the console with the 3 lines repeated over and over and over...

    i've tried this with no luck:

    Java Code:
    public static void main(String[] args) throws Exception{
    
        String text1 = "Old Text";
    
        String text2 = "New Text";
        String sText1 = " is old";
        String sText2 = " is new";
        String tText1 = " now old";
        String tText2 = " now new";
        System.out.print(text1 + "\n" + sText1 + "\n" + tText1 + "\r");
    
        Thread.sleep(600);
        System.out.print(text2 + "\n" + sText2 + "\n" + tText2 + "\r");
    
    }
    and:

    Java Code:
    public static void main(String[] args) throws Exception{
    
        String text1 = "Old Text";
    
        String text2 = "New Text";
        String sText1 = " is old";
        String sText2 = " is new";
        String tText1 = " now old";
        String tText2 = " now new";
        System.out.print(text1 + "\r\n" + sText1 + "\r\n" + tText1 + "\r");
    
        Thread.sleep(600);
        System.out.print(text2 + "\r\n" + sText2 + "\r\n" + tText2 + "\r");
    
    }
    Thanks for any pointers!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Create Command Line Dynamically Updated Lines?

    If you're asking what I think that you're asking: how to change the text (insert in the middle and remove from the middle) of text already posted to the console, then there's a short answer for this: You can't do this using standard Java. For Java the console is too simplistic to allow this to occur. You can get third party libraries that will allow you to do this, or do what I do: code a GUI for situations like this and now you'll have full control of all of the properties of your display text.

  3. #3
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Create Command Line Dynamically Updated Lines?

    Thanks Fubarable... i was afraid of this...

    I'm trying to have this program run on a linux server... which is command line only... so GUI isn't a real possibility...

    Can you recommend any of these third party libraries in particular? Also, just out of curiosity... since these third party libraries are able to accomplish this using the command line, it seems it can be done... possibly in a conveluted way perhaps... but still possible... do you have any insight to how they accomplish this? Thanks again!

    PS: just to make sure you understood what i'm trying to do i guess... once I use "\n" or "println()", the text become static-like and uneditable for that line... and i'm trying to go back and edit/change text in that line afterwards... so i can keep my program confined to 3 lines total instead of repeating lines with the same info over and over.... thanks again :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Create Command Line Dynamically Updated Lines?

    One library that may work (I haven't used it myself mind you) is The JCurses Library. You might want to give it a look and then get back to us to let us know if this helps or not.

Similar Threads

  1. Replies: 2
    Last Post: 10-19-2011, 11:36 AM
  2. Replies: 16
    Last Post: 09-17-2011, 03:41 AM
  3. can i run line by line command in netbean?
    By choconlongxu in forum NetBeans
    Replies: 1
    Last Post: 07-19-2010, 09:41 PM
  4. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 01:23 AM

Posting Permissions

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