0

After getting data from my database, I have to show this in a JTable. In my design I can specify how much rows the JTable should have.

But when my array of data is longer then the specified rows, I get an exception. This is because my table is too small for all the data I want to add in it.

How can I dynamically add rows to my table according to the size of the array?

TableModel tmPerson = taTablePerson.getModel();

    for (int index = 0; index < arrpBag.length; index++)
    {
        dtmPerson.setValueAt(arrpBag[index].getId(), index, 0);
        dtmPerson.setValueAt(arrpBag[index].getRijksregisternummer(), index, 1);
        dtmPerson.setValueAt(arrpBag[index].getNaam(), index, 2);
        dtmPerson.setValueAt(arrpBag[index].getVoornaam(), index, 3);
        dtmPerson.setValueAt(arrpBag[index].getStraat(), index, 4);
        dtmPerson.setValueAt(arrpBag[index].getNummer(), index, 5);
        dtmPerson.setValueAt(arrpBag[index].getBus(), index, 6);
        dtmPerson.setValueAt(arrpBag[index].getPostnummer(), index, 7);
        dtmPerson.setValueAt(arrpBag[index].getGemeente(), index, 8);
        dtmPerson.setValueAt(arrpBag[index].getTelefoonnummer(), index, 9);
    } 

    //Create extra rows when not enough
    if (taTabelPersonen.getRowCount() < arrpBag.length) 
    {
    //What code should be placed here?
    }

    taTabelPersonen.setModel(dtmPersoon);  

1 Answer 1

1

How can I dynamically add rows to my table according to the size of the array?

You can use a DefaultTableModel as table model and call addRow(Object[] rowData) method to dinamically add rows and automatically increasing the row's count. This way you can forget about a fixed row's count. For instance you may have something like this:

Object[] header = new Object[]{"Id", "Rijksregisternummer", "Naam", "Voornaam"
                              ,"Straat", "Nummer", "Bus", "Postnummer"
                              ,"Gemeente", "Telefoonnummer"};

DefaultTableModel model = new DefaultTableModel(header, 0);

for (int index = 0; index < arrpBag.length; index++) {
    Object[] row = new Object[]{ arrpBag[index].getId()
                               , arrpBag[index].getRijksregisternummer()
                               , arrpBag[index].getNaam()
                               , arrpBag[index].getVoornaam()
                               , arrpBag[index].getStraat()
                               , arrpBag[index].getNummer()
                               , arrpBag[index].getBus()
                               , arrpBag[index].getPostnummer()
                               , arrpBag[index].getGemeente()
                               , arrpBag[index].getTelefoonnummer() };

    model.addRow(row);
}

taTabelPersonen.setModel(model);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.