2016-12-04 13 views
1

私は現在プロジェクトを進めており、請求書を表示するにはJTableが必要です。 請求書は形式でテキストファイルに保存されているテキストファイル/配列から2次元配列を作成する

InvoiceID、InvoiceDate、InvoiceCustomer、InvoiceComplete

(int型、文字列、文字列、ブール値)

の他の部分については、私のプログラムでは、各行が読み込まれ、請求書オブジェクトが登録されます。

しかし、私のJTableでは、テキストファイルデータを使って2D配列を作成する方法があまりよく分かりません。私はおそらく配列を最初に作成することでこれをやろうと考えていましたが、あまり確かではありません。私はJTableのチェックボックスを持つことができますので、2次元配列は、オブジェクトの種類の必要が​​ありますので、請求書を格納します。

現在、このタスクの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

新しい回答が追加されました。 [link](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

関連する問題