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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 5 of 5

Thread: Clearing the screen

  1. #1
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Clearing the screen

    Is there any way by which I can clear the screen (terminal window)?
    I used to use the "\u000c" escape sequence in BlueJ and it used to work fine but in JCreator, it displays a box.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Clearing the screen

    In pure Java, no.

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Clearing the screen

    If it is sufficient that my program works on Windows, is there any way ( and more specifically, exceuting it from JCreator) . I mean that my program need not run on all platforms, windows is sufficient.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Clearing the screen

    If you only want to give the appearance of a cleared screen, just print out a lot of new lines. The text will likely still be around, but you'll have to scroll very far up to see it. This is generally not recommended because it's slow and not fool proof.

    Otherwise, I doubt you'll be able to since I believe JCreator uses a GUI textbox element to show the text output and there's no way to access this text box directly without hacking into JCreator (not recommended).

    If your program being run from a terminal (command prompt, bash, xterm, etc.), the best way is to use either JNI or JNA to interface with native C++ code. See Clear the screen - OS Specific Ways for how to write the C++ code.
    Last edited by helloworld922; March 18th, 2012 at 03:52 PM.

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Clearing the screen

    As helloworld922 says, you cannot do this in pure java because java is OS independent and clearing the console is an OS function.

    Whenever I wish to emulate the behavior I use this:

    public void clearScreen() {
        for (int i = 0; i < 16; i++) 
               System.out.println();
    }

    It is dirty and (as above) problematic but it gets the job done. A pure hack.

Similar Threads

  1. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  2. Help with clearing variables
    By fmr in forum Other Programming Languages
    Replies: 2
    Last Post: July 18th, 2011, 10:18 AM
  3. Clearing text from the console
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 5
    Last Post: December 3rd, 2010, 05:36 AM
  4. JTextField clearing problem
    By rushhour in forum AWT / Java Swing
    Replies: 1
    Last Post: October 24th, 2010, 12:55 PM
  5. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 04:00 PM