所以我目前正在开发一个项目,我需要一个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中刚刚还出现空白
'对象[] [] =表新的对象[N] [4];'你既然知道每一列的类型,可以相应地施展他们 – rafid059