我正在编写一个代码,其中数据是从数组的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);
}
//谢谢。