Computer Science Canada

Clearing the Screen

Author:  ProgrammingFun [ Wed Dec 15, 2010 8:56 pm ]
Post subject:  Clearing the Screen

Is there any way of clearing the screen (non-GUI) in java?
I want to use a function similar to cls in Turing but have not been able to find one so far...

Thanks for the help.

Author:  Ultrahex [ Wed Dec 15, 2010 9:44 pm ]
Post subject:  Re: Clearing the Screen

What you are explaining is very vague in Java, if you are talking about console (like System.out.println(...)) and similar then it is not possible really without shell level commands, cause the actual command line interface is not part of Java, but part of the shell in the operating system.

Author:  DemonWasp [ Wed Dec 15, 2010 10:35 pm ]
Post subject:  RE:Clearing the Screen

Plus, it's kinda hard to define what cls should do if your output is redirected to a file.

If you actually want to develop that kind of interface, make a simple GUI with just a text box (not editable) that you use for your display / output, and one smaller one (editable) that they use for input.

Author:  ProgrammingFun [ Thu Dec 16, 2010 6:47 am ]
Post subject:  RE:Clearing the Screen

I haven't learned GUI...
and yes, I am talking about console output...

Author:  DemonWasp [ Thu Dec 16, 2010 9:55 am ]
Post subject:  RE:Clearing the Screen

There's no real way to cls your output. System.out just refers to a PrintWriter, which has no clear functionality -- and for good reason; as I mentioned above, there are lots of contexts where this makes no sense.

You can either write enough lines of output to drive the old output off the screen, or you can not use any kind of clear screen function, or you can use languages with better console support. I would recommend the first or second option.

Author:  Dan [ Thu Dec 16, 2010 5:06 pm ]
Post subject:  RE:Clearing the Screen

Well you can't clear the screen the same way you do in Turing and for good reason (as DemonWasp and others pointed out) you can use ASCII controller characters to manipulate the console a bit.

For example ASCII character 12 (some times denoted as "\f") is form feed and may have a similar effect to clearing the screen (depending on the terminal). I don't have Javac installed on this computer so i can't test but i blive in most terminals it would have the effect similar to outputting blank lines in till it is off the screen and then moving the cursor to the top of the screen.

Another reason not to do this is portability. The action of clearing the screen is not defined in the same in all terminals and operating systems. Well you may get the desired effect in a windows terminal you may just get a mess in a linux terminal.


Edit: I tried typing the form feed char into the Windows 7 terminal and a Linux terminal (running the bash shell) via putty. The result was their was no effect on Windows and the Linux term cleared it's self. This highlights the issues with portability.

Author:  SS1389 [ Fri Dec 17, 2010 12:32 am ]
Post subject:  Re: Clearing the Screen

Is this for some culminating? wink wink Laughing

Author:  ProgrammingFun [ Fri Dec 17, 2010 12:23 pm ]
Post subject:  RE:Clearing the Screen

@SS1389: Yes...it is for the Orthello game Wink
@Everyone else: Thanks for the help. I am developing my program on Win7 and it will be marked on WinXP...so I guess I won't try to do anything fancy with the clearing of the screen.

Author:  SS1389 [ Fri Dec 17, 2010 2:47 pm ]
Post subject:  Re: Clearing the Screen

othello

Author:  SS1389 [ Fri Dec 24, 2010 12:16 pm ]
Post subject:  Re: Clearing the Screen

Yeah, I wanted to clear the screen too, but moving it off the run i/o panel is much easier. That way, it seems like a new update to the game board.

Author:  mirhagk [ Sun Dec 26, 2010 12:30 am ]
Post subject:  RE:Clearing the Screen

couldn't you just \b... I'm not a java programmer but output the backspace char would do the same in most OS i think.

Author:  ProgrammingFun [ Mon Dec 27, 2010 3:56 pm ]
Post subject:  Re: RE:Clearing the Screen

mirhagk @ Sun Dec 26, 2010 12:30 am wrote:
couldn't you just \b... I'm not a java programmer but output the backspace char would do the same in most OS i think.

When I try to output \b, I always get
Result:
  08
as the answer. I used the following as a test case:
Java:

import java.util.*;

public class Test
{
        public static void main (String[] args)
        {
        Scanner sc = new Scanner (System.in);
        System.out.println ("Enter this");
        String fs = sc.next();
       
        System.out.println ("\b");
        }
}

Author:  SS1389 [ Mon Dec 27, 2010 4:09 pm ]
Post subject:  Re: Clearing the Screen

That's because it's the unicode for backspace (\b).

Author:  ProgrammingFun [ Mon Dec 27, 2010 5:06 pm ]
Post subject:  Re: Clearing the Screen

SS1389 @ Mon Dec 27, 2010 4:09 pm wrote:
That's because it's the unicode for backspace (\b).
I realized that Razz ...

Author:  btiffin [ Tue Dec 28, 2010 3:32 am ]
Post subject:  Re: Clearing the Screen

It may not be completely cross platform, but, being old, I have yet to find a console that doesn't support at least some of the ANSI control sequences (outside the glaring Windows/NT console percockle).

Try
code:

final static String CSI = "\0x1B[";
System.out.print(CSI + "2J");
System.out.flush();


Basically escape (ASCII 27) + open square + 2J

The escape + open bracket is usually documented as CSI, Control Sequence Introducer. 2J clears the screen.

And following Dan's advice, ANSI CSI isn't supported on all platforms. So if you plan on your code being used by the world, ignore this.

Cheers

Author:  ProgrammingFun [ Tue Dec 28, 2010 9:18 am ]
Post subject:  Re: Clearing the Screen

Unfortunately, this does not work for me either (I guess I'll just stick to no cls at all Confused )

I tried the following:
Java:

import java.util.*;

public class Test
{
        public static void main (String[] args)
        {
                Scanner sc = new Scanner (System.in);
                System.out.println ("fsfdasfsa");
                String fa = sc.next();
               
                final static String CSI = "\0x1B[";
                System.out.print(CSI + "2J");
                System.out.flush();
               
                String fsaf = sc.next();
               
                System.out.println ("fsafa");
        }
}


I get the following error:
Error:

Test.java:11: modifier static not allowed here
                final static String CSI = "\0x1B[";
                                    ^
1 error


If I try the following:
Java:

import java.util.*;

public class Test
{
        final static String CSI = "\0x1B[";
        public static void main (String[] args)
        {
                Scanner sc = new Scanner (System.in);
                System.out.println ("fsfdasfsa");
                String fa = sc.next();
               
                System.out.print(CSI + "2J");
                System.out.flush();
               
                String fsaf = sc.next();
               
                System.out.println ("fsafa");
        }
}


I get the following output:
input_output:

fsfdasfsa
my input              <---this is what I inputted
 00  x1B[2Jfsafa   <---strangely, this line cannot be copied to the clipboard with Windows

Author:  btiffin [ Tue Dec 28, 2010 9:55 pm ]
Post subject:  Re: Clearing the Screen

Gee; is my lack of Java practice showing up.

I haven't Java'ed in over a decade now.

Perhaps the literal should be '\033[' for the octal equiv? instead of a 0x literal?
Dump the static modifier?

The objective is to create a string with ESCAPE (ascii 27) followed by "[2J". I'll have to leave those technicals to the people here that may actually know. And not us old farts that THINK they know Smile

Cheers

Author:  ProgrammingFun [ Tue Dec 28, 2010 10:00 pm ]
Post subject:  RE:Clearing the Screen

Nope...same problem....
Thanks for the help tho! Smile

Author:  ProgrammingFun [ Thu Dec 30, 2010 9:19 am ]
Post subject:  RE:Clearing the Screen

One more question regarding console....is there any way (non GUI) to give a new popup window in the current console if the user chooses a certain option?
I want the instructions/game rules to popup in a seperate window when the user forgets the rules...

Author:  Tony [ Thu Dec 30, 2010 1:38 pm ]
Post subject:  RE:Clearing the Screen

I thought new windows/tabs spawned new (independent) shells...

Really, it sounds like you are trying to bring too much of GUI mentality over to the console. They are supposed to be different.

Author:  ProgrammingFun [ Thu Dec 30, 2010 2:10 pm ]
Post subject:  RE:Clearing the Screen

So there is no possible way to do this in console?

Author:  Tony [ Thu Dec 30, 2010 3:05 pm ]
Post subject:  RE:Clearing the Screen

You could maybe spawn a new shell and have it run a separate program... I'm murky on the details.

Author:  SS1389 [ Thu Dec 30, 2010 7:56 pm ]
Post subject:  Re: Clearing the Screen

No need for it ProgrammingFun, it won't make the program any better Mr. Green

Author:  ProgrammingFun [ Thu Dec 30, 2010 9:02 pm ]
Post subject:  Re: Clearing the Screen

SS1389 @ Thu Dec 30, 2010 7:56 pm wrote:
No need for it ProgrammingFun, it won't make the program any better Mr. Green

I came up with another way to do it...and yes, it will make it better in one way....

Author:  copthesaint [ Fri Dec 31, 2010 11:46 am ]
Post subject:  RE:Clearing the Screen

The main point of the console screen is for general debugging for your program eg: mathematical problems. Its something that would only be shown in the console and not on the application that is running. o to clear this would be unwise.

For example what if you run this program and it does clear all the text every time, however it also clears warnings and exceptions when the program comes to a runtime error. For example you use the scanner class improperly. I personally think clearing it would be a bad choice. since it is intended for more important uses.

That being said you probably still want to find a way to clear the screen lol.

So I have to ask also, this much effort trying to clear the screen, why not just make a normal application? and you can clear any text all you want Razz

Author:  ProgrammingFun [ Fri Dec 31, 2010 1:45 pm ]
Post subject:  Re: RE:Clearing the Screen

copthesaint @ Fri Dec 31, 2010 11:46 am wrote:
That being said you probably still want to find a way to clear the screen lol.

So I have to ask also, this much effort trying to clear the screen, why not just make a normal application? and you can clear any text all you want Razz

Nah, I'm past the screen-clearing dilemma....solution: Don't clear the screen Mr. Green
I cannot make a normal application because I have not been taught GUI as of yet and the compiler my school uses is not very GUI-friendly...it would be much much easier if I knew GUI

Author:  Tony [ Fri Dec 31, 2010 2:21 pm ]
Post subject:  RE:Clearing the Screen

This will be of interest to this thread
http://twitter.com/#!/camilleroux/status/20913572372549632
code:

ruby -e 'def a;10.times{puts " "*rand(79)+"*"};end;99.times{a;puts " "*34+"Happy New Year 2011";a;sleep 0.1;puts "\e[2J"}'

Author:  TerranceN [ Fri Dec 31, 2010 2:42 pm ]
Post subject:  RE:Clearing the Screen

"the compiler my school uses is not very GUI-friendly"

And what compiler would that be? I know of only sun's JDK and the one in gcc, and I thought they worked the same.

Author:  ProgrammingFun [ Fri Dec 31, 2010 2:55 pm ]
Post subject:  Re: RE:Clearing the Screen

TerranceN @ Fri Dec 31, 2010 2:42 pm wrote:
"the compiler my school uses is not very GUI-friendly"

And what compiler would that be? I know of only sun's JDK and the one in gcc, and I thought they worked the same.

My school uses jGRASP, which according to my teacher, is not the best thing to use with GUI...but I myself have run GUI programs using it....

Author:  SS1389 [ Fri Dec 31, 2010 3:07 pm ]
Post subject:  Re: Clearing the Screen

I'm sure you have.

Author:  ProgrammingFun [ Fri Dec 31, 2010 6:47 pm ]
Post subject:  Re: Clearing the Screen

@SS1389: You sound sarcastic...

How can I use the code given below to open a file which is in the same directory as the Java file?
Java:

Runtime.getRuntime().exec( "cmd.bat /E:1900 /C MyCmd.cmd" );


I was trying to open a file in notepad (or Word if it can work) so I did the following:
Java:

Runtime.getRuntime().exec( "notepad.exe /C:1900 /C test.txt" );


This opens up notepad but it cannot find the file which I have defined.
What do I need to do with this part:
Java:

/E:1900 /C test.txt


so that it can open up the file in the same directory as the program?

Author:  jcollins1991 [ Fri Dec 31, 2010 7:05 pm ]
Post subject:  Re: Clearing the Screen

I don't see any reason to need "/E:1900 /C test.txt". It's probably just executing something as it would in the command line, so just doing "notepad.exe FILE" or "notepad.exe C:\\PATH\\TO\\FILE".

Author:  ProgrammingFun [ Fri Dec 31, 2010 7:21 pm ]
Post subject:  Re: Clearing the Screen

jcollins1991 @ Fri Dec 31, 2010 7:05 pm wrote:
I don't see any reason to need "/E:1900 /C test.txt". It's probably just executing something as it would in the command line, so just doing "notepad.exe FILE" or "notepad.exe C:\\PATH\\TO\\FILE".
I believe that part defines which file to open (though I am not sure)
If I just leave it as "notepad.exe", only a new instance of notepad will be opened whereas I want to open a txt file for the user to read....

Author:  SS1389 [ Fri Dec 31, 2010 8:32 pm ]
Post subject:  Re: Clearing the Screen

It was a bit sarcastic... Mr. Green

Author:  copthesaint [ Fri Dec 31, 2010 11:38 pm ]
Post subject:  Re: RE:Clearing the Screen

Tony @ Fri Dec 31, 2010 wrote:
This will be of interest to this thread
http://twitter.com/#!/camilleroux/status/20913572372549632
code:

ruby -e 'def a;10.times{puts " "*rand(79)+"*"};end;99.times{a;puts " "*34+"Happy New Year 2011";a;sleep 0.1;puts "\e[2J"}'


lol kinda random tony :p

Author:  Tony [ Fri Dec 31, 2010 11:41 pm ]
Post subject:  RE:Clearing the Screen

It clears a console's screen with "\e[2J"

Author:  ProgrammingFun [ Sun Jan 02, 2011 8:00 pm ]
Post subject:  Re: Clearing the Screen

jcollins1991 @ Fri Dec 31, 2010 7:05 pm wrote:
I don't see any reason to need "/E:1900 /C test.txt". It's probably just executing something as it would in the command line, so just doing "notepad.exe FILE" or "notepad.exe C:\\PATH\\TO\\FILE".
Oh....I get it now...thanks for the help.


: