java 从数组填充 JTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13123811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:35:25  来源:igfitidea点击:

Populating JTable from array

javaswingjtable

提问by user1667191

Been looking around but couldn't find anything that explain how to do something like this:

环顾四周,但找不到任何可以解释如何执行以下操作的内容:

Object rowData[] = { "Row1-Column1", "Row1-Column2", "Row1-Column3" };

Object columnNames[] = { "Column One", "Column Two", "Column Three" };

I already made a table with the editor, name: "jTable1".

我已经用编辑器制作了一个表格,名称:“jTable1”。

回答by Dan D.

Something like this will help you:

像这样的事情会帮助你:

DefaultTableModel model = new DefaultTableModel(columnNames, 0);
model.addRow(rowData);
jTable1.setModel(model);

回答by dogbane

You need a two-dimensional array for the row data. Try this:

您需要一个用于行数据的二维数组。试试这个:

Object rowData[][] = {{ "Row1-Column1", "Row1-Column2", "Row1-Column3" }};
Object columnNames[] = { "Column One", "Column Two", "Column Three" };
JTable table = new JTable(rowData, columnNames);