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 6 of 6

Thread: Clearing text from the console

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Clearing text from the console

    Okay so I'm making a program that is text-based and will print out a lot of text to the screen. I am using a batch file to run the program and I was wondering if there is any command to clear the screen in the sense that "cls" clears the batch window when creating a batch file.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Clearing text from the console

    This isn't a topic which arises much in a Java environment, however it does in the C/C++ environment. The topic of clearing text from the console is a very sore point.

    The long and short of it is, you should never clear the console window.


    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    sp11k3t3ht3rd (December 2nd, 2010)

  4. #3
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Clearing text from the console

    Alright. I just figured it would look nicer. Thanks for the help!

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Clearing text from the console

    Your Welcome,

    I Understand that principle, but trust me it's not worth it.

    Think about the cmd window, when does microsoft ever clear it hehe

    Chris

  6. #5
    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 text from the console

    Well, when you ask the user to enter something like a password it'd be nice to clear the console I can't remember why Java was never implemented to allow the programmer to clear the console, but I think it had something to do with cross-platform issues.

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Clearing text from the console

    Yes it does raise cross platform issues, as to do it you must target the OS for its commands to do so.
    For example on windows, this is how you would go about it.

    Console.h
    #include <jni.h>
    /* Header for class Console */
     
    #ifndef _Included_Console
    #define _Included_Console
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     Console
     * Method:    clr
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_Console_clr
      (JNIEnv *, jobject);
     
    #ifdef __cplusplus
    }
    #endif
    #endif
    Console.c
    #include <windows.h>
    #include <jni.h>
     
    #ifdef __cplusplus
    extern "C" {
    #endif
    	JNIEXPORT void JNICALL Java_Console_clr(JNIEnv *, jobject){
     
           /*
           * Get Console HANDLE for control
           */
     
           HANDLE chwnd = GetStdHandle(STD_OUTPUT_HANDLE);
     
           COORD coordScreen = { 0, 0 };
           DWORD cCharsWritten;
     
     
            /*
            * Ensure text colours are stored and set
            */
           {
               WORD textColours;
               CONSOLE_SCREEN_BUFFER_INFO *consoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();
               GetConsoleScreenBufferInfo(chwnd, consoleInfo);
               textColours = consoleInfo->wAttributes;
               SetConsoleTextAttribute(chwnd, textColours);
           }
     
           CONSOLE_SCREEN_BUFFER_INFO csbi;
           DWORD dwConSize;
     
     
            /*
            * If fail to get Console Dimensions, return
            */
     
           if( !GetConsoleScreenBufferInfo( chwnd, &csbi ))
              return;
           dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
     
            /*
            * Return on error when filling the screen with ' '
            */
     
           if( !FillConsoleOutputCharacter( chwnd, (TCHAR) ' ',
              dwConSize, coordScreen, &cCharsWritten ))
              return;
     
           if( !GetConsoleScreenBufferInfo( chwnd, &csbi ))
              return;
     
           if( !FillConsoleOutputAttribute( chwnd, csbi.wAttributes,
              dwConSize, coordScreen, &cCharsWritten ))
              return;
     
           /*
           * Return Cursor to Origin of Console
           */
     
           SetConsoleCursorPosition( chwnd, coordScreen );
     
           return;
    	}
     
    #ifdef __cplusplus
    }
    #endif
    Compile with under G++
     

    g++ -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I"C:/Program Files/Java/jdk1.6.0_14/include" -I"C:/Program Files/Java/jdk1.6.0_14/include/win32" -shared ConsoleClear.c -o ConsoleClear.dll




    Console.java
    /*
     * Usage: Console.clr()
     */
     
    public class Console {
     
    	/*
    	 * Load External DLL for clr Function
    	 */
    	static {
    		System.loadLibrary("ConsoleClear");
    	}
     
    	public static native void clr();
    }

    Example usage
    import java.util.Scanner;
     
    public class Example {
     
    	public static void main(String[] args) {
    		System.out.print("Hello, shall I clear the screen? (y/n) :> ");
    		if(new Scanner(System.in).nextLine().equalsIgnoreCase("y")){
    			Console.clr();
    			System.out.println("Console Cleared!");
    		}else{
    			System.out.println("Console not cleared!");
    		}
     
    	}
     
    }

    The reason why clearing is frowned upon is because applications running in the console can quite often me running just after other applcations have been executed, and clearing the console should not be done because it will clear the output of the previous applications data.

    Chris

Similar Threads

  1. Console Application - How to run it?
    By mirzahat in forum AWT / Java Swing
    Replies: 3
    Last Post: November 16th, 2010, 12:21 AM
  2. JTextField clearing problem
    By rushhour in forum AWT / Java Swing
    Replies: 1
    Last Post: October 24th, 2010, 12:55 PM
  3. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 04:00 PM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM