I am writing a code in which data is collected from an arraylist of arrays, which I add to a JTable. However, the code always adds the data bottom-side up, so if there are two rows of data, it adds them to the last two rows, instead of the first two. Here’s is the relavent code,
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);
}
//thanks.
Change
model.addRow(objects);tomodel.insertRow(0, objects);.