Note that there are some explanatory texts on larger screens.

plurals
  1. POJTable not getting populated properly
    primarykey
    data
    text
    <p>So after many many edits to my questions, I am still unable to solve this problem. I have a .txt file called ProcessList.txt which is populated every time ps -e command executes inside my Java app. Here's the code I used to redirect the output of ps -e to ProcessList.txt.</p> <pre><code>import java.io.*; import java.util.StringTokenizer; public class GetProcessList { private String GetProcessListData() { Process p; Runtime runTime; String process = null; try { System.out.println("Processes Reading is started..."); //Get Runtime environment of System runTime = Runtime.getRuntime(); //Execute command thru Runtime // p = runTime.exec("tasklist"); // For Windows p=runTime.exec("ps -e"); //For Linux //Create Inputstream for Read Processes InputStream inputStream = p.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); //Read the processes from sysrtem and add &amp; as delimeter for tokenize the output String line = bufferedReader.readLine(); process = "&amp;"; while (line != null) { line = bufferedReader.readLine(); process += line + "&amp;"; } //Close the Streams bufferedReader.close(); inputStreamReader.close(); inputStream.close(); System.out.println("Processes are read."); } catch (IOException e) { System.out.println("Exception arise during the read Processes"); e.printStackTrace(); } return process; } void showProcessData() { try { //Call the method For Read the process String proc = GetProcessListData(); //Create Streams for write processes //Given the filepath which you need.Its store the file at where your java file. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("ProcessList.txt")); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); //Tokenize the output for write the processes StringTokenizer st = new StringTokenizer(proc, "&amp;"); while (st.hasMoreTokens()) { bufferedWriter.write(st.nextToken()); //Write the data in file bufferedWriter.newLine(); //Allocate new line for next line } //Close the outputStreams bufferedWriter.close(); outputStreamWriter.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } </code></pre> <p>It made a file in my workspace called ProcessList.txt. It looks something like this having 4 columns:</p> <pre><code>1 ? 00:00:00 init 2 ? 00:00:00 kthreadd 3 ? 00:00:00 ksoftirqd/0 5 ? 00:00:00 kworker/u:0 6 ? 00:00:00 migration/0 </code></pre> <p>Now once the ProcessList.txt file is created, I redirected the contents of this .txt file to JTable as:</p> <pre><code>import java.io.*; import java.awt.*; import java.util.*;import javax.swing.*; import java.awt.event.*; import javax.swing.table.*; public class InsertFileToJtable extends AbstractTableModel{ Vector data; Vector columns; private String[] colNames = {"&lt;html&gt;&lt;b&gt;PID&lt;/b&gt;&lt;/html&gt;","&lt;html&gt;&lt;b&gt;TTY&lt;/b&gt;&lt;/html&gt;","&lt;html&gt;&lt;b&gt;time&lt;/b&gt;&lt;/html&gt;","&lt;html&gt;&lt;b&gt;Process Name&lt;/b&gt;&lt;/html&gt;",}; public InsertFileToJtable() { String line; data = new Vector(); columns = new Vector(); try { FileInputStream fis = new FileInputStream("ProcessList.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); StringTokenizer st1 = new StringTokenizer(br.readLine(), " "); while (st1.hasMoreTokens()) columns.addElement(st1.nextToken()); while ((line = br.readLine()) != null) { StringTokenizer st2 = new StringTokenizer(line, " "); while (st2.hasMoreTokens()) data.addElement(st2.nextToken()); } br.close(); } catch (Exception e) { e.printStackTrace(); } } public int getRowCount() { return data.size() / getColumnCount(); } public int getColumnCount() { return columns.size(); } public Object getValueAt(int rowIndex, int columnIndex) { return (String) data.elementAt((rowIndex * getColumnCount()) + columnIndex); } @Override public String getColumnName(int column) { return colNames[column]; } @Override public Class getColumnClass(int col){ return getValueAt(0,col).getClass(); } } </code></pre> <p>Here's how my output look so far (I would have uploaded the screen shot of the final output if I knew how to :( but here's a little idea of what the output Jtable looks so far.</p> <pre><code>============================= PID TTY TIME PROCESS NAME ============================= 2 ? 00:00:00 kthreadd 3 ? 00:00:00 ksoftirqd/0 5 ? 00:00:00 kworker/u:0 6 ? 00:00:00 migration/0 </code></pre> <p>Note that the contents of ProcessList.txt get redirected to the JTAble except the fact that the first line of ProcessList.txt file i.e</p> <pre><code> 1 ? 00:00:00 init </code></pre> <p>does not seem to be in the JTable output. Any help would be highly appreciated. I have posted many questions about this but came out without luck :(</p> <p>EDIT: Here's my constructor and main()</p> <pre><code>public void InterfaceFrame(){ setTitle("My Frame"); add(tabbedPane); Pack(); setVisible(true); </code></pre> <p>} </p> <pre><code>public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { panel.setSize(100,100); panel.add(table); model.fireTableStructureChanged(); table.setModel(model); InsertFileToJtable model = new InsertFileToJtable(); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); RowSorter&lt;TableModel&gt; sorter = new TableRowSorter&lt;TableModel&gt;(model); table.setRowSorter(sorter); JScrollPane scrollpane = new JScrollPane(table); panel.add(scrollpane, BorderLayout.CENTER); JButton button = new JButton("Show View"); panel.add( button, BorderLayout.SOUTH ); tabbedPane.addTab("Process",null,scrollpane,""); //////////////SOME OTHER TABS/////////////////////////// } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload