Forums Register Login

How can I clear the screen?

+Pie Number of slices to send: Send
Hi... I was wrighting a program, and it has several ouputs. I was using
System.out.println("Bla bla bla");
But when I run it in the second time the ouput from the first run stays on the screen, how can I clear it before running? Is there any method like "clrscr;" in Pascal?
+Pie Number of slices to send: Send
Dear Ilya
I came to know the way how to clear the screen on the DOS window through this forum. There is not a pre-defined method as you mentioned but this may be called from DOS thru Runtime class.
One of the way mentioned in that email is as follows. You can search that message about which I mentioned earlier for another way.

Good luck
Rashid Ali
+Pie Number of slices to send: Send
Thank you, for the advice, but it just doesn't seem to work out that way... dunno why. But thanx anyway!
+Pie Number of slices to send: Send
I had the same problem with the call the command line argument "cls" thru java this is what I did instead.
private static void clear() {
for (int y = 0; y < 25; y++)
System.out.println("\n");
}
This method will place 25 newlines to the screen. Since the screens are '24'x'80' This seems to work ok. Now if someone can tell me why I can call a batch file in dos thru java and it works great but can not call a script thru HP-UX 11.
------------------
Sun Certified Programmer on the Java 2 Platform
+Pie Number of slices to send: Send
i am also a student of java.
i agree with tom.
but i think that's not the way.
use flush();
i will send you code of that.
within a day.
+Pie Number of slices to send: Send
will following code place 25 newline characters or 50 new line characters??

Originally posted by Tom Ben:
I had the same problem with the call the command line argument "cls" thru java this is what I did instead.
private static void clear() {
for (int y = 0; y < 25; y++)
System.out.println("\n");
}
This method will place 25 newlines to the screen. Since the screens are '24'x'80' This seems to work ok. Now if someone can tell me why I can call a batch file in dos thru java and it works great but can not call a script thru HP-UX 11.


+Pie Number of slices to send: Send
I would say that 50 new lines would be written. One for each println() and one for each new line ( "\n" ).
+Pie Number of slices to send: Send
It prints 50.
see for youself :

+Pie Number of slices to send: Send
Yes it prints 50 sorry I was in a hurry and typed println not print. Thanks for clearing that up. Did that flush() option work?
------------------
Sun Certified Programmer on the Java 2 Platform
+Pie Number of slices to send: Send

waiting for code

Originally posted by dwivedi_sachin5:
i am also a student of java.
i agree with tom.
but i think that's not the way.
use flush();
i will send you code of that.
within a day.


+Pie Number of slices to send: Send
flush() is used in IO to clear the streams. It will not clear a console - only a "pipe".
It depends on your OS. You need to use whatever command works from the propmt on that OS.
This works for NT/Win2000 and I think 98
Runtime.getRuntime().exec( "cmd /c cls" ) ;
Before that (win95) you had to use
Runtime.getRuntime().exec( "command /c cls" ) ;
I am not sure what works for Unix.
This obviously will make the app OS specific, therefore a better bet is just to print out a bunch (maybe 30 or so) of newline characters to the console, which forces the stuff on the console to scroll up.
+Pie Number of slices to send: Send
Cindy hints at it above, but just to be clear Java's console is not yet robust enough to provide screen clearing functionality.
+Pie Number of slices to send: Send
 

Originally posted by Rashid Ali:
[B]Dear Ilya
I came to know the way how to clear the screen on the DOS window through this forum. There is not a pre-defined method as you mentioned but this may be called from DOS thru Runtime class.
One of the way mentioned in that email is as follows. You can search that message about which I mentioned earlier for another way.

Good luck
Rashid Ali[/B]


When I run your program, there are some errors.why?
Exception in thread "main" java.io.IOException: CreateProcess: command /c cls er
ror=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Unknown Source)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Test.main(Test.java:3)
+Pie Number of slices to send: Send
Cindy pointed out that the DOS command are specific to the Operating system. Try using cmd instead of command.

This works for NT/Win2000 and I think 98
Runtime.getRuntime().exec( "cmd /c cls" ) ;
Before that (win95) you had to use
Runtime.getRuntime().exec( "command /c cls" ) ;
+Pie Number of slices to send: Send
Hi All,
I am using Windows 98 and using either of the commands,
Runtime.getRuntime().exec( "command /c cls" ) ;
or
Runtime.getRuntime().exec( "cmd /c cls" ) ;
is throwing an IOException.
Kezia.
+Pie Number of slices to send: Send
 

Originally posted by Cindy Glass:
This works for NT/Win2000 and I think 98
Runtime.getRuntime().exec( "cmd /c cls" ) ;


I tried this - the program does not throw any exception, but does not clear the screen either!

+Pie Number of slices to send: Send
There have been quite a number of responses quoting a way to "clear the screen", but everything about the screen is operating-system specific (and even version-specific as we've seen in the discussions of MS-Windows-95 vs MS-Windows-NT).

A more portable way is to create a JFrame with a JTextArea or JTextPane in it. You then append to it instead of printing.

You can call its setText method with a null String, i.e.,
<pre>myTextArea.setText("")</pre> to clear then screen.

------------------
Ian Darwin,
Author of Java Cookbook: Solutions and Examples for Java Developers
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 29874 times.
Similar Threads
how to clear the dos propmt from a java program
Empty space caused by rendered attribute
class name in choices.txt
string search & replace
Spring MVC Flow
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:40:30.