[英]Java - Arraylist contents to display in Jtable
我正在从事一个项目,并且在这个领域上处于困境。 我正在从文件中读取文本,并将其保存到arraylist中。 问题是来自文件的内容出现在jtable中的一行文本中,但是我希望每一行都以行显示。 我正在传递来自另一个类的数据,并且我知道这是可行的,因为我可以在控制台的每一行看到打印出的内容。 我尝试了几种不同的方法,但是我的想法已经用完了。 任何帮助表示赞赏。
以下是我编写的代码。
for (String item : helper.getItems() )
{
System.out.println(item);
storage.add(item);
}
JTable t1 = new JTable();
t1.setModel(new DefaultTableModel(
new Object[][]{
{storage.toString()}
},
new String[]{
"Tool Equipment"
}
));
storage.toString()
将为您提供ArrayList
字符串表示形式。 您想要的可能是List#toArray
将项目加入一个最好的方式JTable
使用DefaultTableModel
对象是利用.addRow
所述的方法DefaulTableModel
。 您需要解析storage
字符串,以确定要放入每行/列的值
例如:
DefaultTableModel model=new DefaultTableModel();
// parsing of the storage obj, to get individual values for each col/row
// depending on the structure of storage a looping construct would be beneficial here
String col1= storage.substring(startindex, endindex);
String col2=storage.substring(startindex, endindex);
//add items to the model in a new row
model.addRow(new Object[] {col1, col2});
// if you used a loop to parse the data in storage, end it here
// add model to the table
t1.setModel(model);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.