spa*_*dfs 1 java swing jtable arraylist object
我正在编写一个代码,其中从数组的arraylist收集数据,我将其添加到JTable中.但是,代码总是将数据添加到底部,因此如果有两行数据,则将它们添加到最后两行,而不是前两行.这是相关代码,
public class RegistrationView extends GUIDesign implements ActionListener {
//variable declarations here.
public RegistrationView (GTPort gport){
super("Student Report", 3);
gtPort = gport;
setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
setPreferredSize(new Dimension(500,800));
header = new JPanel();
header.setSize(getWidth(), 30);
body = new JPanel();
body.setLayout(new BoxLayout(body,BoxLayout.Y_AXIS));
header.setBackground(color);
title.setFont(new Font("Helvetica", 1,20));
header.add(title);
data = new Object[15][4];
model = new DefaultTableModel(data, columns);
table = new JTable(model);
body.add(table.getTableHeader());
body.add(table);
backButton.addActionListener(this);
buttonPanel.add(backButton);
buttonPanel.add(backButton);
add(header);
add(body);
add(buttonPanel);
}
public void refresh()
{
//this is the data to be added. it is an arraylist of object arrays.
ArrayList<Object[]> selectedData = gtPort.selectedData;
//i use this code to erase all the data in the table, if there is any. this //method may be called later, so the data must be erased.
model.setRowCount(0);
table = new JTable(model);
model.setRowCount(35);
//adding the rows to the model, which is then added to the table.
for (Object[] objects: selectedData)
{
model.addRow(objects);
}
table = new JTable(model);
}
Run Code Online (Sandbox Code Playgroud)
//谢谢.
| 归档时间: |
|
| 查看次数: |
4419 次 |
| 最近记录: |