从文本文件/数组

时间:2016-12-04 21:53:06

标签: java arrays multidimensional-array jtable

所以我目前正在开发一个项目,我需要一个JTable来显示发票。 发票以

格式存储在文本文件中
  

InvoiceID,InvoiceDate,InvoiceCustomer,InvoiceComplete

     

(int,String,String,boolean)

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

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

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

更新:

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仍然是空白

1 个答案:

答案 0 :(得分: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();
        }