participate


Java Programming [Archive] - Clear screen?
<<   Back to Forum  |   Give us Feedback
This topic has 18 replies on 2 pages.    1 | 2 | Next »
khoonchee
Posts:14
Registered: 1/11/02
Clear screen?   
Mar 7, 2002 6:46 PM

 
Hi....
is there any way to clear screen in java such as the one in C languange..System("cls");??????Do I need to import any class ???
thanks...
 
goguest
Posts:35
Registered: 3/2/02
Re: Clear screen?   
Mar 7, 2002 8:45 PM (reply 1 of 18)  (In reply to original post )

 
*************************************************************
char esc = 27;
String clear = esc + "[2J";
System.out.print(clear);

And this line must be inside the config.sys:
device=c:\windows\command\ansi.sys
*************************************************************

Hi, I've already posted a question similar to yours few days ago. Luckily, I found a code that can clear off the console DOS screen in Window. However, I have big trouble in understanding the mechanism behind the code and desperately need some advice from all experts in this forum. Here is all the questions:

1) Why do I need this token "[2J"? What is it used for?
2) Why must I load the driver "ANSI.SYS" in order to have the program to work properly?
3) What is the mechanism that drives the program to clear off the DOS screen?
 
khoonchee
Posts:14
Registered: 1/11/02
Re: Clear screen?   
Mar 8, 2002 12:04 AM (reply 2 of 18)  (In reply to #1 )

 
thanks for that...
 
cknelsen
Posts:996
Registered: 1/8/99
Re: Clear screen?   
Mar 11, 2002 9:45 AM (reply 3 of 18)  (In reply to #1 )

 
<escape> + "[2J" is the ANSI escape sequence for clearing the screen.

For windoze, ANSI escape sequences will not work properly unless the ANSI device driver is loaded.
 
tommyCheng
Posts:1
Registered: 5/6/02
Re: Clear screen?   
May 6, 2002 10:51 AM (reply 4 of 18)  (In reply to #3 )

 
I seems doesn't work on NT, what can I do??
have any other methods??
thx
 
thedracle
Posts:419
Registered: 8/25/01
Re: Clear screen?   
May 6, 2002 11:33 AM (reply 5 of 18)  (In reply to #4 )

 
Wow, as miss cleo would say, " That's simply amazin' ". I never knew that this even existed, sweetness, thankyou very much!

-Jason Thomas.
 
hanxue
Posts:101
Registered: 2/9/00
Re: Clear screen?   
Jun 22, 2002 11:12 PM (reply 6 of 18)  (In reply to #5 )

 
I tried, but the output is:

<--[2J<--[2J


Note: <-- is representing an arrow pointing towards left (only one character)
 
Zemthematress42
Posts:224
Registered: 8/20/01
Re: Clear screen?   
Jun 22, 2002 11:39 PM (reply 7 of 18)  (In reply to #6 )

 
I tried, but the output is:

<--[2J<--[2J


Note: <-- is representing an arrow pointing towards
left (only one character)


If I'm not mistaken, I do believe the ibm Ascii Character 27 is indeed a left arrow...and the MS ascii 27 is ESC
 
hanxue
Posts:101
Registered: 2/9/00
Re: Clear screen?   
Jun 23, 2002 9:33 AM (reply 8 of 18)  (In reply to #7 )

 
Okay, then what is the ESC character for IBM ASCII?
 
MHanauska
Posts:21
Registered: 11/15/99
Re: Clear screen?   
Jun 23, 2002 5:28 PM (reply 9 of 18)  (In reply to original post )

 
You usually don't write command line applications in Java. That's just for really basic stuff or if it must run via a telnet connection or so, for everything that counts as real application, make your own command line window. Here's a "dirty", but simple one class implementation (usually one would use anonymous classes, but I wanted to keep it simple), that opens an own command line window, prints out some text, clears the screen after a break and after another break quits gracefully.

Compile with
javac Test.java

Start with either:
java Test

or on Windows you can use:
javaw Test

So you can close your DOS box and application continues running.

// File Test.java
import java.awt.*;
import java.awt.event.*;
 
public class Test extends Frame implements WindowListener {
    
    private final TextArea TA = new TextArea
            ("", 25, 80, TextArea.SCROLLBARS_NONE);
    private final String NEWLINE = "" + '\n';
    
    public Test() {
        TA.setEditable(false);
        add(TA);
        setTitle("Test Application");
        setResizable(false);
        addWindowListener(this);
        pack();
        setVisible(true);
        // The last methods were inherited
        // because we extend java.awt.Frame
    }
    
    public void print(String s) {
        TA.append(s);
    }
    
    public void println(String s) {
        TA.append(s);
        TA.append(NEWLINE);
    }
    
    public void cls() {
        TA.setText("");
    }
    
    public void kill() {
        dispose();
    }
    
    
    // We must implement the following as we are
    // implementing WindowListener
    
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
    
    public void windowActivated(WindowEvent e) {}
    
    public void windowClosed(WindowEvent e) {
        System.exit(0);
    }
    
    public void windowClosing(WindowEvent e) {
        dispose();
    }
            
    
    public static void main(String[] args) {
        Test t = new Test();
        t.println("Hello World!");
        t.print("Same line ");
        t.println("Test");
        try {
            Thread.sleep(5000); // Wait 5 seconds
        } catch (InterruptedException e) {
            // Do nothing, not critical to
            // be interrupted
        }
        t.cls();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // Do nothing, not critical to
            // be interrupted
        }
        t.kill();        
    }
}
 
madmango
Posts:3
Registered: 1/8/03
Re: Clear screen?   
Jan 8, 2003 1:39 PM (reply 10 of 18)  (In reply to #9 )

 
Hey... that's cool and it works!

Alrighty - i'm extremely new to Java, i'm taking an intro to Java course in high school.

how would one go about getting input from such a text area? I'm cool getting input from a regular console, just not this fancy new thingey.
 
madmango
Posts:3
Registered: 1/8/03
Re: Clear screen?   
Jan 8, 2003 1:40 PM (reply 11 of 18)  (In reply to #9 )

 
Hey... that's cool and it works!

Alrighty - i'm extremely new to Java, i'm taking an intro to Java course in high school.

how would one go about getting input from such a text area? I'm cool getting input from a regular console, just not this fancy new thingey.
 
mlk
Posts:10,967
Registered: 8/15/01
Re: Clear screen?   
Jan 8, 2003 3:12 PM (reply 12 of 18)  (In reply to #11 )

 
Java does not support esc codes by default, you can get packages to do this for you:
http://www.pitman.co.za/projects/charva/
http://www.nongnu.org/jcurzez/

Google and this forum have a lot more information on this subject, I recomend searching for ncurses.
 
Prorok
Posts:166
Registered: 11/25/02
Re: Clear screen?   
Jan 8, 2003 3:27 PM (reply 13 of 18)  (In reply to #12 )

 
cant you use dos command?
for Windows
Runtime.getRuntime().exec("command cls");
or
Runtime.getRuntime().exec("cmd cls");
 
madmango
Posts:3
Registered: 1/8/03
Re: Clear screen?   
Jan 9, 2003 12:39 PM (reply 14 of 18)  (In reply to #13 )

 
No, all this does is open up a new window, clear it, and close that window. I'm talking about clearing the parent process.
 
This topic has 18 replies on 2 pages.    1 | 2 | Next »
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 29
  • Guests : 132

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums