Clear the Screen

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    Clear the Screen

    Hi,

    I have done some research, trying to Clear The Screen in java code.

    The first option was the obv:
    system.out.prin t("\n\n\n\n\n\n \n\n\n\n\n\n");

    then i heard about this method:
    System.out.prin t((char)27 + "[2J");

    However, it doen't work unless "ansi.sys" is loaded and very few WinXP
    user's have this.

    Any help appreciated in trying to solve this problem.

    Cheers,
    Dave


  • Nathan Zumwalt

    #2
    Re: Clear the Screen

    It's kind of a hack, but what about this:

    Runtime.exec("c ls");

    This makes your application platform-dependent, but it looks like your
    stated alternative does this as well.

    -Nathan

    "Dave" <bigdavepotnood le*SPAM*hotmail .com> wrote in message news:<3ffd55bd$ 0$13355$cc9e4d1 f@news-text.dial.pipex .com>...[color=blue]
    > Hi,
    >
    > I have done some research, trying to Clear The Screen in java code.
    >
    > The first option was the obv:
    > system.out.prin t("\n\n\n\n\n\n \n\n\n\n\n\n");
    >
    > then i heard about this method:
    > System.out.prin t((char)27 + "[2J");
    >
    > However, it doen't work unless "ansi.sys" is loaded and very few WinXP
    > user's have this.
    >
    > Any help appreciated in trying to solve this problem.
    >
    > Cheers,
    > Dave[/color]

    Comment

    • Denz

      #3
      Re: Clear the Screen

      Ive seen this question before- with no real solution.
      Surely it wouldnt have been difficult to provide a clear screen, and even
      basic screen cursor positioning?
      It would make text-mode java alot more useful.

      "Dave" <bigdavepotnood le*SPAM*hotmail .com> wrote in message
      news:3ffd55bd$0 $13355$cc9e4d1f @news-text.dial.pipex .com...[color=blue]
      > Hi,
      >
      > I have done some research, trying to Clear The Screen in java code.
      >
      > The first option was the obv:
      > system.out.prin t("\n\n\n\n\n\n \n\n\n\n\n\n");
      >
      > then i heard about this method:
      > System.out.prin t((char)27 + "[2J");
      >
      > However, it doen't work unless "ansi.sys" is loaded and very few WinXP
      > user's have this.
      >
      > Any help appreciated in trying to solve this problem.
      >
      > Cheers,
      > Dave
      >
      >[/color]


      Comment

      • Silvio Bierman

        #4
        Re: Clear the Screen


        "Dave" <bigdavepotnood le*SPAM*hotmail .com> wrote in message
        news:3ffd55bd$0 $13355$cc9e4d1f @news-text.dial.pipex .com...[color=blue]
        > Hi,
        >
        > I have done some research, trying to Clear The Screen in java code.
        >
        > The first option was the obv:
        > system.out.prin t("\n\n\n\n\n\n \n\n\n\n\n\n");
        >
        > then i heard about this method:
        > System.out.prin t((char)27 + "[2J");
        >
        > However, it doen't work unless "ansi.sys" is loaded and very few WinXP
        > user's have this.
        >
        > Any help appreciated in trying to solve this problem.
        >
        > Cheers,
        > Dave
        >
        >[/color]

        Look at jcurses on sourceforge.

        Silvio Bierman


        Comment

        • Christian

          #5
          Re: Clear the Screen

          Printing out the form-feed character will clear the screen

          System.out.prin t("\f");

          Hope this helps!

          Christian

          Comment

          • Anthony Borla

            #6
            Re: Clear the Screen


            "Denz" <RUBBISH@RUBBIS Hhotmail.com> wrote in message
            news:KvlLb.1671 $Wa.792@news-server.bigpond. net.au...[color=blue]
            >
            > Ive seen this question before- with no real solution.
            > Surely it wouldnt have been difficult to provide a clear
            > screen, and even basic screen cursor positioning?
            >
            > It would make text-mode java alot more useful.
            >[/color]

            Unfortunately these tasks are inherently operating system-specific, and
            cannot be implemented in any standard way across platforms. Some platforms,
            in fact, don't even understand the concepts of 'screen', or 'cursor', so
            wouldn't even have any use for classes that implemented these abstractions.
            Even the 'standard' C and C++ languages which, like Java, aim for platform
            independance, do not implement such functionality.

            System-specific tasks such as these may be performed via:

            * 'Runtime.getRun time().exec(... )'

            * Writing Java Native Interface [JNI] routines which tap into
            the relevant operating system functionality

            The first option should really be seen as a 'quick and dirty' approach - it
            allows you to perform certain system-specific tasks but with very little
            control. A program, to be considered robust, would make no more than sparing
            use of this facility. If you find you are relying on it heavily you may need
            to seriously question whether Java is the ideal development tool for this
            task [a scripting language (Perl, Python) might be more suitable ?], or more
            of a development commitment needs be made by using JNI.

            There is nothing inherently wrong with JNI except that its use helps defeat
            one of Java's most important qualities - platform independance - and it
            introduces 'weaknesses' into the application because program execution
            occurs outside the control of the JVM, and any failure here could be
            catastrophic. On the flipside, Java would be a very limited development
            environment without it since it allows for the tapping into system-specific
            functionality in a controlled, and efficient way.

            So, to provide the facilities you seek, you need to implement suitable JNI
            routines. Again, options are available:

            * Tap into someone else's work. As another respondant has
            already meantioned, the JCurses package provides a
            reasonable suit of screen handling routines for the *NIX
            and Windows-familiy platforms

            * Write your own. This isn't as difficult as you might imagine,
            and might be the best approach if all you require is a
            few simple routines like:

            - Clearing the screen
            - Moving the cursor to specified positions
            - Accepting input [say a single keystroke] without pressing
            ENTER

            A good start would be the JNI tutorial at the Sun site, and,
            of course, perusal of the documentation for the system
            functions implementing this functionality

            Anyway, apologies for the longwindedness of the response. Hopefully, though,
            some useful insights have been provided.

            Cheers,

            Anthony Borla


            Comment

            • Anthony Borla

              #7
              Re: Clear the Screen


              "Christian" <javauser@email user.net> wrote in message
              news:32330a83.0 401081855.47512 efc@posting.goo gle.com...[color=blue]
              >
              > Printing out the form-feed character will clear the screen
              >
              > System.out.prin t("\f");
              >
              > Hope this helps!
              >[/color]

              Sadly, it doesn't do as you expect - it's behaviour various with the
              platform. Nice try, though :)

              Cheers,

              Anthony Borla


              Comment

              • Anthony Borla

                #8
                Re: Clear the Screen


                "Nathan Zumwalt" <nathanz@hotmai l.com> wrote in message
                news:521673fd.0 401081330.49023 c22@posting.goo gle.com...[color=blue]
                > It's kind of a hack, but what about this:
                >
                > Runtime.exec("c ls");
                >
                > This makes your application platform-dependent, but it looks
                > like your stated alternative does this as well.
                >[/color]

                Actually, this won't work. Yes, the 'cls' command is invoked, *but* it is
                invoked in a new process ['command.com' or 'cmd.exe' is exected creating a
                new process], and *does not* clear the Java application's console.

                Cheers,

                Anthony Borla


                Comment

                • nos

                  #9
                  Re: Clear the Screen

                  But if they can implement "beep", and they do,
                  they should be able to implement "cls" too.

                  "Anthony Borla" <ajborla@bigpon d.com> wrote in message
                  news:g0yLb.2893 $Wa.1516@news-server.bigpond. net.au...[color=blue]
                  >
                  > "Denz" <RUBBISH@RUBBIS Hhotmail.com> wrote in message
                  > news:KvlLb.1671 $Wa.792@news-server.bigpond. net.au...[color=green]
                  > >
                  > > Ive seen this question before- with no real solution.
                  > > Surely it wouldnt have been difficult to provide a clear
                  > > screen, and even basic screen cursor positioning?
                  > >
                  > > It would make text-mode java alot more useful.
                  > >[/color]
                  >
                  > Unfortunately these tasks are inherently operating system-specific, and
                  > cannot be implemented in any standard way across platforms. Some[/color]
                  platforms,[color=blue]
                  > in fact, don't even understand the concepts of 'screen', or 'cursor', so
                  > wouldn't even have any use for classes that implemented these[/color]
                  abstractions.[color=blue]
                  > Even the 'standard' C and C++ languages which, like Java, aim for platform
                  > independance, do not implement such functionality.
                  >
                  > System-specific tasks such as these may be performed via:
                  >
                  > * 'Runtime.getRun time().exec(... )'
                  >
                  > * Writing Java Native Interface [JNI] routines which tap into
                  > the relevant operating system functionality
                  >
                  > The first option should really be seen as a 'quick and dirty' approach -[/color]
                  it[color=blue]
                  > allows you to perform certain system-specific tasks but with very little
                  > control. A program, to be considered robust, would make no more than[/color]
                  sparing[color=blue]
                  > use of this facility. If you find you are relying on it heavily you may[/color]
                  need[color=blue]
                  > to seriously question whether Java is the ideal development tool for this
                  > task [a scripting language (Perl, Python) might be more suitable ?], or[/color]
                  more[color=blue]
                  > of a development commitment needs be made by using JNI.
                  >
                  > There is nothing inherently wrong with JNI except that its use helps[/color]
                  defeat[color=blue]
                  > one of Java's most important qualities - platform independance - and it
                  > introduces 'weaknesses' into the application because program execution
                  > occurs outside the control of the JVM, and any failure here could be
                  > catastrophic. On the flipside, Java would be a very limited development
                  > environment without it since it allows for the tapping into[/color]
                  system-specific[color=blue]
                  > functionality in a controlled, and efficient way.
                  >
                  > So, to provide the facilities you seek, you need to implement suitable JNI
                  > routines. Again, options are available:
                  >
                  > * Tap into someone else's work. As another respondant has
                  > already meantioned, the JCurses package provides a
                  > reasonable suit of screen handling routines for the *NIX
                  > and Windows-familiy platforms
                  >
                  > * Write your own. This isn't as difficult as you might imagine,
                  > and might be the best approach if all you require is a
                  > few simple routines like:
                  >
                  > - Clearing the screen
                  > - Moving the cursor to specified positions
                  > - Accepting input [say a single keystroke] without pressing
                  > ENTER
                  >
                  > A good start would be the JNI tutorial at the Sun site, and,
                  > of course, perusal of the documentation for the system
                  > functions implementing this functionality
                  >
                  > Anyway, apologies for the longwindedness of the response. Hopefully,[/color]
                  though,[color=blue]
                  > some useful insights have been provided.
                  >
                  > Cheers,
                  >
                  > Anthony Borla
                  >
                  >[/color]


                  Comment

                  • Stewart Gordon

                    #10
                    Re: Clear the Screen

                    While it was 9/1/04 3:04 pm throughout the UK, nos sprinkled little
                    black dots on a white screen, and they fell thus:
                    [color=blue]
                    > But if they can implement "beep", and they do,
                    > they should be able to implement "cls" too.[/color]
                    <snip top of upside-down reply>

                    That's because BEL is a standard ASCII character (code 7) and it's
                    pretty much standard that any terminal/console would render it by
                    beeping. OTOH, for some reason I can't imagine FF (12) doesn't
                    standardly mean clear screen, and nor does any other ASCII string.

                    But I agree that having a portable means of screen clearing would be
                    handy. BTW, have the rest of you people stopped telling people to try
                    what you can't be bothered to try for yourself?

                    Stewart.

                    --
                    My e-mail is valid but not my primary mailbox, aside from its being the
                    unfortunate victim of intensive mail-bombing at the moment. Please keep
                    replies on the 'group where everyone may benefit.

                    Comment

                    • Jared Dykstra

                      #11
                      Re: Clear the Screen

                      "Denz" <RUBBISH@RUBBIS Hhotmail.com> wrote in message news:<KvlLb.167 1$Wa.792@news-server.bigpond. net.au>...[color=blue]
                      > Ive seen this question before- with no real solution.
                      > Surely it wouldnt have been difficult to provide a clear screen, and even
                      > basic screen cursor positioning?
                      > It would make text-mode java alot more useful.
                      >[/color]

                      Changing the cursor positions, clearing the screen, setting console
                      colurs...these are all functions of the console in use, not of Java.
                      One of the clear screen solutions touched on this by feeding the
                      correct escape sequence to the console to manipulate it with the
                      desired effect.

                      As you can imagine, this is tedious and time consuming. JCurses has
                      already been mentioned. It is an implementation of nCurses which
                      determines the console type being used, and wraps escape sequences
                      with a nice API, independant of the type of console being used.

                      It should be here:
                      Download Java Curses Library for free. JCurses, a java console windowing toolkit for Windows and Linux. The Java Curses Library (JCurses) is a library for developing text terminal based applications using Java programming language. It is implemented as a Windowing toolkit similar to AWT, but built upon the UNIX "curses" windowing system.


                      However, I can't help but wonder that if you need this type of
                      functionality from the console, perhaps you should consider the more
                      standard Java/Swing and make a nice GUI interface to your application.

                      ---
                      Jared Dykstra

                      Comment

                      • Denz

                        #12
                        Re: Clear the Screen

                        mmm... interesting
                        Just tried- it works on the Linux box, but unfortunately it doesnt on
                        Windows- just prints a gender symbol. darn.
                        Going to look at jcurses

                        "Christian" <javauser@email user.net> wrote in message
                        news:32330a83.0 401081855.47512 efc@posting.goo gle.com...[color=blue]
                        > Printing out the form-feed character will clear the screen
                        >
                        > System.out.prin t("\f");
                        >
                        > Hope this helps!
                        >
                        > Christian[/color]


                        Comment

                        • Tony Morris

                          #13
                          Re: Clear the Screen



                          --
                          Tony Morris
                          (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
                          Software Engineer
                          IBM Australia - Tivoli Security Software


                          "Dave" <bigdavepotnood le*SPAM*hotmail .com> wrote in message
                          news:3ffd55bd$0 $13355$cc9e4d1f @news-text.dial.pipex .com...[color=blue]
                          > Hi,
                          >
                          > I have done some research, trying to Clear The Screen in java code.
                          >
                          > The first option was the obv:
                          > system.out.prin t("\n\n\n\n\n\n \n\n\n\n\n\n");
                          >
                          > then i heard about this method:
                          > System.out.prin t((char)27 + "[2J");
                          >
                          > However, it doen't work unless "ansi.sys" is loaded and very few WinXP
                          > user's have this.
                          >
                          > Any help appreciated in trying to solve this problem.
                          >
                          > Cheers,
                          > Dave
                          >
                          >[/color]


                          Comment

                          • Anthony Borla

                            #14
                            Re: Clear the Screen


                            "Stewart Gordon" <smjg_1998@yaho o.com> wrote in message
                            news:btmocn$cis $1@sun-cc204.lut.ac.uk ...[color=blue]
                            > While it was 9/1/04 3:04 pm throughout the UK, nos sprinkled little
                            > black dots on a white screen, and they fell thus:
                            >[color=green]
                            > > But if they can implement "beep", and they do,
                            > > they should be able to implement "cls" too.[/color]
                            > <snip top of upside-down reply>
                            >
                            > That's because BEL is a standard ASCII character
                            > (code 7) and it's pretty much standard that any terminal/console
                            > would render it by beeping.
                            >[/color]

                            Try it on an IBM mainframe - they use EBCDIC, not ASCII ;) !
                            [color=blue]
                            >
                            > OTOH, for some reason I can't imagine FF (12) doesn't
                            > standardly mean clear screen, and nor does any other
                            > ASCII string.
                            >[/color]

                            You'll find that 'plain vanilla' *NIX / Linux systems *do* exhibit fairly
                            consistent console behaviour; it's the Windows-family environments that
                            exhibit such inconsistencies .
                            [color=blue]
                            >
                            > But I agree that having a portable means of screen clearing
                            > would be handy.
                            >[/color]

                            Maybe a nested class could be added to 'System', containing all the
                            system-specific console-management routines, something like:

                            class System
                            {
                            ...
                            public class Console
                            {
                            ...
                            public static native void cls();
                            public static native void setCurPos(int row, int col);
                            public static native String inputString();
                            public static native double inputNumeric();
                            ...
                            }
                            ...
                            }

                            You could then do something like:

                            System.Console. cls();

                            to clear the screen, and something like:

                            String name;

                            System.Console. setCurPos(10, 35);
                            System.out.prin t("Enter your name: ");
                            name = System.Console. inputString();

                            Platforms that did not support consoles or the like could then simply have
                            dummy [i.e. empty] methods ?

                            Maybe if the Sun Java developers were lobbied by enough developers they
                            might consider building this functionality into the API ?

                            I suspect, though, they will simply leave it to developers requiring console
                            management support to use JNI, either directly, or by downloading a package
                            like JCurses.
                            [color=blue]
                            >
                            > BTW, have the rest of you people stopped telling people
                            > to try what you can't be bothered to try for yourself?
                            >[/color]

                            I think their intentions were sincere, and probably did do some testing on
                            their own systems. The error was in assuming that the same behaviour could
                            be expected on other systems. My general view is that any constructive
                            response is to be welcomed and appreciated.

                            Cheers,

                            Anthony Borla


                            Comment

                            • nos

                              #15
                              Re: Clear the Screen


                              "Stewart Gordon" <smjg_1998@yaho o.com> wrote in message
                              news:btmocn$cis $1@sun-cc204.lut.ac.uk ...[color=blue]
                              > While it was 9/1/04 3:04 pm throughout the UK, nos sprinkled little
                              > black dots on a white screen, and they fell thus:
                              >[color=green]
                              > > But if they can implement "beep", and they do,
                              > > they should be able to implement "cls" too.[/color]
                              > <snip top of upside-down reply>
                              >
                              > That's because BEL is a standard ASCII character (code 7) and it's
                              > pretty much standard that any terminal/console would render it by
                              > beeping. OTOH, for some reason I can't imagine FF (12) doesn't
                              > standardly mean clear screen, and nor does any other ASCII string.
                              >[/color]
                              I thought I would try to find out how beep works by looking in the java
                              sdk files but i could not figure it out. All i found was

                              public abstract void beep();
                              in java/awt/Toolkit.java
                              [color=blue]
                              > But I agree that having a portable means of screen clearing would be
                              > handy. BTW, have the rest of you people stopped telling people to try
                              > what you can't be bothered to try for yourself?
                              >
                              > Stewart.
                              >
                              > --
                              > My e-mail is valid but not my primary mailbox, aside from its being the
                              > unfortunate victim of intensive mail-bombing at the moment. Please keep
                              > replies on the 'group where everyone may benefit.[/color]


                              Comment

                              Working...