
-----------------------------------
ProgrammingFun
Wed Dec 15, 2010 8:56 pm

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.

-----------------------------------
Ultrahex
Wed Dec 15, 2010 9:44 pm

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.

-----------------------------------
DemonWasp
Wed Dec 15, 2010 10:35 pm

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.

-----------------------------------
ProgrammingFun
Thu Dec 16, 2010 6:47 am

RE:Clearing the Screen
-----------------------------------
I haven't learned GUI...
and yes, I am talking about console output...

-----------------------------------
DemonWasp
Thu Dec 16, 2010 9:55 am

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.

-----------------------------------
Dan
Thu Dec 16, 2010 5:06 pm

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.

-----------------------------------
SS1389
Fri Dec 17, 2010 12:32 am

Re: Clearing the Screen
-----------------------------------
Is this for some culminating? wink wink  :lol:

-----------------------------------
ProgrammingFun
Fri Dec 17, 2010 12:23 pm

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.

-----------------------------------
SS1389
Fri Dec 17, 2010 2:47 pm

Re: Clearing the Screen
-----------------------------------
othello

-----------------------------------
SS1389
Fri Dec 24, 2010 12:16 pm

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.

-----------------------------------
mirhagk
Sun Dec 26, 2010 12:30 am

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.

-----------------------------------
ProgrammingFun
Mon Dec 27, 2010 3:56 pm

Re: 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.
When I try to output \b, I always get   08 as the answer.  I used the following as a test case:

import java.util.*;

public class Test
{
	public static void main (String

-----------------------------------
SS1389
Mon Dec 27, 2010 4:09 pm

Re: Clearing the Screen
-----------------------------------
That's because it's the unicode for backspace (\b).

-----------------------------------
ProgrammingFun
Mon Dec 27, 2010 5:06 pm

Re: Clearing the Screen
-----------------------------------
That's because it's the unicode for backspace (\b).I realized that  :P ...

-----------------------------------
btiffin
Tue Dec 28, 2010 3:32 am

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
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

-----------------------------------
ProgrammingFun
Tue Dec 28, 2010 9:18 am

Re: Clearing the Screen
-----------------------------------
Unfortunately, this does not work for me either (I guess I'll just stick to no cls at all  :? )

I tried the following:

import java.util.*;

public class Test
{
	public static void main (String

I get the following error:

Test.java:11: modifier static not allowed here
		final static String CSI = "\0x1B

If I try the following:

import java.util.*;

public class Test
{
	final static String CSI = "\0x1B

I get the following output:

fsfdasfsa
my input              