Search code examples
javamodeljtablejdatechooser

How to add multiple columns using model.addRow?


I want to add three variables into three different columns of a row but am running into a problem as I don't know how to add multiple columns at once with model.addRow.

My main problem is that I have to use model.addRow in order to convert my jDateChooser variable into one that can be input into the jTable and see no way of adapting it to my need.

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    String theDate = dateFormat.format(jDateChooser1.getDate());
    DefaultTableModel model = (DefaultTableModel)jTable1.getModel();

    String Name = jTextField1.getText();

    model.addRow(new Object[]{theDate});

'Name' is one of the variables that I want to put into another column, but I see no way of getting it into the table as it is. The code above is under the jButtonActionPerformed event.


Solution

  • You need to add all the values to the columns in the table. Suppose you have 4 columns and you need to add a row with 3rd value equal to name and rest empty.

    model.addRow(new Object[] {"", "", name, ""});
    

    Adding just 1 column does not help