2016-12-04 103 views
1

所以我目前正在开发一个项目,我需要一个JTable来显示发票。 发票都存储在一个文本文件中的格式从文本文件/数组创建二维数组

InvoiceID,InvoiceDate,InvoiceCustomer,InvoiceComplete

(INT,字符串,字符串,布尔)

对于其他部分我程序,每行都被读入并创建发票对象。

但是,对于我的JTable,我不太清楚如何使用文本文件数据创建二维数组。我想也许试图通过首先创建一个数组来做到这一点,但我并不太确定。二维数组需要是Object类型的,因为它将存储发票,所以我可以在JTable中有一个复选框。

我目前刚刚有一个带有JTable的空类,用于此任务。任何意见,将不胜感激。由于

UPDATE:

JPanel invoiceViewPanel= new JPanel(null); //layout 

Object data[][]= new Object[4][10]; 
String columnHeaders[]={"Invoice ID","Invoice Name","Customer", "Complete?"}; 

DefaultTableModel model = new DefaultTableModel(data, columnHeaders) { 

boolean[] Editable= new boolean[]{ 
       false, false, false, true 
     }; 

     public boolean isCellEditable(int rowIndex, int columnIndex) { 
      return editableCells[columnIndex]; 
     } 

@Override 
public Class<?> getColumnClass(int columnIndex) 
{ 
    return columnClass[columnIndex]; 
} 
}; 

JTable table=new JTable(model); 

JScrollPane tableContainer=new JScrollPane(table);  

final Class[] columnClass = new Class[] 
{ 
Integer.class, String.class, String.class, Boolean.class 
}; 

public void launch() 
{ 
this.setLayout(new FlowLayout()); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  


this.add(invoiceViewPanel); 
invoiceViewPanel.add(tableContainer); 
createObject(); 
this.add(tableContainer); 

this.setTitle("Invoices"); 
this.setSize(500,600); 
this.setVisible(true); 
this.setResizable(false); 

} 

public void tableData() 
{ 
try 
      { 
       FileReader reader = new FileReader("Invoices.txt"); 
       BufferedReader bReader = new BufferedReader(reader); 

       String sReadline = bReader.readLine(); 
       int x=0; 

       while(sReadline!=null) 
       { 
        String[] invoiceData= sReadline.split(",");  

        data[x][0]=Integer.parseInt(invoiceData[0]); 
        data[x][1]=invoiceData[1]; 
        data[x][2]=invoiceData[2]; 
        data[x][3]=Boolean.parseBoolean(invoiceData[1]); 

        x=x+1; 

        sReadline=bReader.readLine();//sreadline 
       } 
      } 

      catch (Exception e) 
      { 
       System.out.println(e); 
      } 

} 

虽然我已经单独尝试分配数组值,无论我得到一个ArrayIndexOutOfBoundsException或JTable中刚刚还出现空白

+0

'对象[] [] =表新的对象[N] [4];'你既然知道每一列的类型,可以相应地施展他们 – rafid059

回答

0

我可以说,先阅读和迭代通过该文本文件的行并向您的JTable实例执行addRow()。

List<String[]> rowList = new ArrayList<String[]>(); 
FileInputStream fstream = new FileInputStream("yourfile.txt"); 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String strLine; 
     while ((strLine = br.readLine()) != null) { 
     try{ 
     String[] tokens = str.split(" "); 
     //process record 
     rowList.add(tokens); 

     /*either add to row list first and then use the global variable 
     for your job or simply add row to that jTable instance 
     right here */ 

     } 
     catch (Exception e){ 
     e.printStacktrace(); 
     } 
     } 
    in.close(); 
+0

我不知道这是否是相同的,但我尝试了类似的东西。我有一个缓冲读取器,并为每一行我创建一个阵列的对象。但是,当我添加它时,它仍然不会出现在JTable上。 – user9123

+0

我已添加新的帮助答案。 [链接](http://stackoverflow.com/a/40964571/4370219) – Smit

0

用这个希望代替你的代码它会有所帮助。

  JPanel invoiceViewPanel= new JPanel(null); //layout 
     List<String[]> rowList = new ArrayList<String[]>(); 
     //Object data[][]= new Object[4][10]; 
     String columnHeaders[]={"Invoice ID","Invoice Name","Customer", "Complete?"}; 

     DefaultTableModel model = new DefaultTableModel(convertToArray, columnHeaders) { 

     boolean[] Editable= new boolean[]{ 
         false, false, false, true 
       }; 

       public boolean isCellEditable(int rowIndex, int columnIndex) { 
        return editableCells[columnIndex]; 
       } 

       private Object convertToArray(){ 
        String[][] array = new String[list.size()][2]; 
        int i = 0; 
        for (String[] nestedList : rowList) { 
         array[i++] = {nestedList[0],nestedList[1]}; 
        } 
        /* 
        you can add dynamic column no. here i added 2 
        */ 
        return array; 
       } 
     @Override 
     public Class<?> getColumnClass(int columnIndex) 
     { 
      return columnClass[columnIndex]; 
     } 
     }; 

     JTable table=new JTable(model); 

     JScrollPane tableContainer=new JScrollPane(table);  

     final Class[] columnClass = new Class[] 
     { 
     Integer.class, String.class, String.class, Boolean.class 
     }; 

     public void launch() 
     { 
     this.setLayout(new FlowLayout()); 
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  


     this.add(invoiceViewPanel); 
     invoiceViewPanel.add(tableContainer); 
     createObject(); 
     this.add(tableContainer); 

     this.setTitle("Invoices"); 
     this.setSize(500,600); 
     this.setVisible(true); 
     this.setResizable(false); 

     } 

     public void tableData() 
     { 
      FileInputStream fstream = new FileInputStream("yourfile.txt"); 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      String strLine; 
      while ((strLine = br.readLine()) != null) { 
      try{ 
      String[] tokens = str.split(" "); 
      //process record 
      rowList.add(tokens); 

      /*either add to row list first and then use the global variable 
      for your job or simply add row to that jTable instance 
      right here */ 

      } 
      catch (Exception e){ 
      e.printStacktrace(); 
      } 
      } 
     in.close(); 
     } 
+0

所以,我只是在rowList.add中添加整个数组?为什么没有分裂? – user9123

+0

分割只是一个分隔符,您的文本文件中可能会有一些分隔符。 – Smit