JTable没有正确填充

时间:2012-12-03 05:04:59

标签: java swing file-io ubuntu-12.04

因此,在对我的问题进行了许多编辑之后,我仍然无法解决这个问题。我有一个名为ProcessList.txt的.txt文件,每次在我的Java应用程序中执行ps -e命令时都会填充该文件。这是我用来将ps -e的输出重定向到ProcessList.txt的代码。

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 & as delimeter for tokenize the output
 String line = bufferedReader.readLine();
 process = "&";
 while (line != null) {
 line = bufferedReader.readLine();
 process += line + "&";
 }

 //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, "&");

 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();
 }

 }
}

它在我的工作区中创建了一个名为ProcessList.txt的文件。它看起来像这样有4列:

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

现在创建ProcessList.txt文件后,我将此.txt文件的内容重定向到JTable:

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 = {"<html><b>PID</b></html>","<html><b>TTY</b></html>","<html><b>time</b></html>","<html><b>Process Name</b></html>",};


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();
}
}

这是我的输出到目前为止的样子(如果我知道如何:我会上传最终输出的屏幕截图:(但是这里有点了解到目前为止输出Jtable的内容。

=============================
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    

请注意,ProcessList.txt的内容被重定向到JTAble,除了ProcessList.txt文件的第一行,即

    1 ?        00:00:00 init

似乎不在JTable输出中。任何帮助将受到高度赞赏。我已经发布了很多关于此的问题,但没有运气就出来了:(

编辑:这是我的构造函数和main()

public void InterfaceFrame(){
setTitle("My Frame");
add(tabbedPane);
Pack();
setVisible(true);

}

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<TableModel> sorter =
              new TableRowSorter<TableModel>(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///////////////////////////
}

1 个答案:

答案 0 :(得分:1)

  

[...] ProcessList.txt的内容被重定向到JTAble,但ProcessList.txt文件的第一行[...]似乎不在{{1}输出。

这是因为您正在读取JTable向量的第一行,其他所有向columns向量的行。我假设您采用它的核心使用了带有标题行的输入文件。只需将所有内容读入数据,只需计算整数中的列即可获得列数。

例如,您可以像这样编辑代码,data表示已移除的行,-表示已添加的行。

+
相关问题