sparkonhdfs
sparkonhdfs

Reputation: 1343

Adding rows to JTable in the right order.

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.

Upvotes: 3

Views: 5150

Answers (3)

Amarnath
Amarnath

Reputation: 8865

Adding row at the bottom of the table because DefaultTableModel will add row at the end if you use .. addRow(..) method as stated in the javadoc. Use insertRow(..) method to insert row at the specific location. But take care of ArrayOutOfBoundException.

As stated in the javadoc,

 public void insertRow(int row, Object[] rowData)

Inserts a row at row in the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.

 public void addRow(Object[] rowData)

Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.

Upvotes: 1

shaunw
shaunw

Reputation: 370

It looks as if you are adding rows after you add the elements to the JTable by setting the row count twice. It should populate for you instead of having to set the row count. And to refresh your table after changes, try using fireTableDataChanged(); and check out http://docs.oracle.com/javase/tutorial/uiswing/components/table.html on how to properly structure your layout.

Upvotes: 1

tckmn
tckmn

Reputation: 59273

Change model.addRow(objects); to model.insertRow(0, objects);.

Upvotes: 10

Related Questions