0

Trying to clear the output window in NetBeans: CTRL + L works, and so I am trying to recreate that command using awt.Robot. I've looked around and tried System.out.flush(); which didn't work. I don't want to print new lines.

Here is my test code:

package test;

// import Robot, KeyEvent & AwtException
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class Test {

    public static void main(String[] args) throws AWTException, InterruptedException {

        // print text to output
        System.out.println("Hello World!");

        // initialize new robot, "keyPresser"
        Robot keyPresser = new Robot();

        // hold & release CTRL + L to theoretically clear the output window
        keyPresser.keyPress(KeyEvent.VK_CONTROL);
        Thread.sleep(500);
        keyPresser.keyPress(KeyEvent.VK_L);
        Thread.sleep(500);

        keyPresser.keyRelease(KeyEvent.VK_CONTROL);
        Thread.sleep(500);
        keyPresser.keyRelease(KeyEvent.VK_L);
        Thread.sleep(500);
    }

}

Am I on the right track? Is this the wrong way to go about this? What other options could I use to clear the output window?

rohan
  • 36
  • 3
  • Netbeans will need to have key board focus for this to work, so maybe add a `Thread.sleep` BEFORE you start issuing commands and make are you click Netbeans so it has keyboard focus first – MadProgrammer Jun 17 '19 at 22:39
  • Alternatively, you can make the Robot focus Netbeans by itself. The class has mouse manipulation capability. – cbryant02 Jun 17 '19 at 23:12
  • Did you try this to clear the output? It work on Windows console and unix/linux terminal, VSCode output also. But I didn't test on Netbean yet. https://stackoverflow.com/questions/19252496/clear-screen-with-windows-cls-command-in-java-console-application – Esc Điệp Jun 18 '19 at 00:03

0 Answers0