简体   繁体   中英

Populating JTable from array

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".

Something like this will help you:

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM