Real'sHowTo AddThis Feed Button
Custom Search

Clear the console and control attributesTag(s): IO Open Source

There is no built-in way to really control character-based application in Java.

To clear the screen, you can use many System.out.println();, that's about it!

for (int i=0; i<25; i++)

... Or use some JNI functions, see this HowTo.

While it is possible to use the DOS ANSI.SYS driver with Windows 9x and make old JVM use it ... it won't work with modern Windows installations. Windows NT (or better) CMD.EXE does not support ANSI escape sequences at all.

Fortunately many Open Source solutions are coming to the rescue and Jansi and JCurses are two of them.


Jansi is a small java library that allows you to use ANSI escape sequences to format your console output which works even on Windows.
import org.fusesource.jansi.AnsiConsole;

public class Test {
  public static final String ANSI_CLS = "\u001b[2J";
  public static final String ANSI_HOME = "\u001b[H";
  public static final String ANSI_BOLD = "\u001b[1m";
  public static final String ANSI_AT55 = "\u001b[10;10H";
  public static final String ANSI_REVERSEON = "\u001b[7m";
  public static final String ANSI_NORMAL = "\u001b[0m";
  public static final String ANSI_WHITEONBLUE = "\u001b[37;44m";

  public static void main(String args[]){
       (ANSI_AT55 + ANSI_REVERSEON + "Hello world" + ANSI_NORMAL);
       (ANSI_HOME + ANSI_WHITEONBLUE + "Hello world" + ANSI_NORMAL);
       (ANSI_BOLD + "Press a key..." + ANSI_NORMAL);
    try {;}catch(Exception e){}
NOTE: Check this "old" text file to have an overview of ANSI Escape Sequences.

The above example shows that it's possible to use ANSI codes directly but Jansi provides a neat mechanism to help building the required ANSI sequence.

  import static org.fusesource.jansi.Ansi.*;
  import static org.fusesource.jansi.Ansi.Color.*;
  System.out.println( ansi().eraseScreen().fg(RED).a("Hello").fg.(GREEN).a(" World").reset() )

Jansi works on Linux32/64, Windows 32/64 and OS/X.


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.

JCurses works on Unix and Windows (32 bit only, on a 64-bit OS you need to use JCurses with a 32-bit JVM).

This example will display a character-based window with a label, a textfield and a button (don't click with you mouse, use the keyboard!).

import jcurses.system.*;
import jcurses.widgets.*;
import jcurses.util.*;
import jcurses.event.*;

public class Test2 extends Window implements ItemListener, ActionListener,
    ValueChangedListener, WindowListener, WidgetsConstants {
  static Test2 window = null;
  static TextField textfield = null;
  static Button button = null;

  public Test2(int width, int height) {
    super(width, height, true, "JCurses Test");

  public static void main(String[] args) throws Exception {
    window = new Test2(30, 20);

  public void init() {
    DefaultLayoutManager mgr = new DefaultLayoutManager();
        new Label("Hello World!",
                  new CharColor(CharColor.WHITE, CharColor.GREEN)),
                  0, 0, 20, 10,

    textfield = new TextField(10);
    mgr.addWidget(textfield, 0, 0, 20, 20,

    button = new Button("Quit");
    mgr.addWidget(button, 0, 0, 20, 30,

    window.addListener((WindowListener) this);;

  public void actionPerformed(ActionEvent event) {
    Widget w = event.getSource();
    if (w == button) {
      new Message("HowTo", "You are about to quit", "OK").show();

  public void stateChanged(ItemEvent e) {  }

  public void valueChanged(ValueChangedEvent e) {  }

  public void windowChanged(WindowEvent event) {
    if (event.getType() == WindowEvent.CLOSING) {
      // Toolkit.clearScreen(new CharColor(CharColor.WHITE, CharColor.BLACK));

blog comments powered by Disqus

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2015
[ home ]