Question

The code is fairly simple , which show currently running processes

 Process p = Runtime.getRuntime().exec("TASKLIST");

 BufferedReader reader = new BufferedReader(new InputStreamReader(
 p.getInputStream()));
 String line;

while ((line = reader.readLine()) != null) {
System.out.println(line);
   }

output on console i m getting in proper format

     Image Name             PID Session Name Session#    Mem Usage
     ========================== ===================== ============
     System Idle Process      0 Services            0         24 K
     System                   4 Services            0      1,260 K
     svchost.exe            896 Services            0      7,060 K
     taskmgr.exe            868 Console             2     13,300 K
     WINWORD.EXE           5412 Console             2     39,860 K
     iexplore.exe          5256 Console             2     69,104 K
     eclipse.exe           1112 Console             2      1,788 K
     javaw.exe             4380 Console             2    555,552 K
     cmd.exe               1500 Console             2      3,264 K
     conhost.exe           3120 Console             2      7,188 K
     bash.exe              3360 Console             2      7,840 K

to show the output of the code on Jtextarea instead of

   System.out.println(line);

i did

   textarea.append(line+"\n");

but the formatting shows on text area is not proper as on console ,

    Image Name                     PID Session Name        Session#    Mem Usage
    ========================= ======== ================ =========== ============
    System Idle Process      0 Services                     0  4 K
    System                      4 Services                0 10,024 K
    smss.exe                  324 Services                  0  312 K
    csrss.exe                      436 Services            02,252 K

----------------------------------UPDATE-------------------------------

Formatting issue resolved by changing the font of Textarea , now its properly aligned

Was it helpful?

Solution

Seems that unlike the console, the font used by textarea is not Monospaced Font.

Either set the JTextArea's font to a monospaced font (or the logical Font.MONOSPACED), or use a JTable to display your data.

Check out this answer too

OTHER TIPS

You may want to use a JTable, using a JTable everything is in rows and columns like a spreadsheet. It may require more code than you already have but rest assured it will work (if programmed correctly)

To start off with you will need a 2-dimensional array like this:

private String[][] data = {
        {"Example 1", "0.0.1", "Update"},
        {"Example 2", "0.0.1", "Installed"}
};

Once you have this you will need a array of columns like this:

private String[] columns = {"Mod name", "Version", "Installed"};

Once you have both of these you can use this to make the table:

JTable table = new JTable(data, columns);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top