How to add row dynamically in JTable

2019-02-06 17:02发布

问题:

I want to add the row dynamically in JTable and I have writen the following code for that:

    tblTaskList = new JTable();
    tblTaskList.setShowVerticalLines(false);
    tblTaskList.setCellSelectionEnabled(true);
    tblTaskList.setColumnSelectionAllowed(true);
    tblTaskList.setBorder(new LineBorder(null));
    for (int count = 1; count <= 10; count++) {
        tblTaskList.setModel(new DefaultTableModel(new Object[][] { {
                count, "title1", "start", "stop", "pause", "status" }, },
                new String[] { "status", "Task Title", "Start", "Stop",
                        "Pause", "Status" }));
    }
    tblTaskList.getColumnModel().getColumn(0).setPreferredWidth(31);
    tblTaskList.getColumnModel().getColumn(1).setPreferredWidth(346);
    tblTaskList.getColumnModel().getColumn(2).setPreferredWidth(33);
    tblTaskList.getColumnModel().getColumn(3).setPreferredWidth(31);
    tblTaskList.getColumnModel().getColumn(4).setPreferredWidth(28);

    tblTaskList.setBounds(93, 34, 614, 160);
    frmTaskList.getContentPane().add(tblTaskList);

The problem is that only the last row is added, i.e. count print the value 10 in first column,,,can anyone explain how to solve the problem?

回答1:

 // create object of table and table model
 JTable tbl = new JTable();
 DefaultTableModel dtm = new DefaultTableModel(0, 0);

// add header of the table
String header[] = new String[] { "Prority", "Task Title", "Start",
            "Pause", "Stop", "Statulses" };

// add header in table model     
 dtm.setColumnIdentifiers(header);
   //set model into the table object
       tbl.setModel(dtm);

     // add row dynamically into the table      
for (int count = 1; count <= 30; count++) {
        dtm.addRow(new Object[] { "data", "data", "data",
                "data", "data", "data" });
 }


回答2:

In your loop you always set a new TableModel to JTabel. Instead of that you need to create one instance of model, and then add rows to that. Use next code :

DefaultTableModel model = new DefaultTableModel(new String[] { "status", "Task Title", "Start", "Stop", "Pause", "Status" },0);
tblTaskList.setModel(model);
for (int count = 1; count <= 10; count++) {
     model.addRow(new Object[]{ count, "title1", "start", "stop", "pause", "status" });
}

Instead of:

for (int count = 1; count <= 10; count++) {
    tblTaskList.setModel(new DefaultTableModel(new Object[][] { {
            count, "title1", "start", "stop", "pause", "status" }, },
            new String[] { "status", "Task Title", "Start", "Stop",
                    "Pause", "Status" }));
}


回答3:

i Have also found one solution for this and it runs successfully

    DefaultTableModel dm = new DefaultTableModel(0, 0);
    String header[] = new String[] { "Status", "Task Title", "Start",
            "Pause", "Stop", "Statulses" };
    dm.setColumnIdentifiers(header);
    tblTaskList.setModel(dm);

    for (int count = 1; count <= 10; count++) {
        Vector<Object> data = new Vector<Object>();
        data.add(count);
        data.add("Project Title" + count);
        data.add("Start");
        data.add("Stop");
        data.add("Pause");
        data.add("Status");
        System.out.println("test :- " + count);
        dm.addRow(data);
    }


回答4:

 DefaultTableModel model ;
    /** Creates new form DynRowAdd */
    public DynRowAdd() {
        initComponents();
          model = new DefaultTableModel();
                    jTable1.setModel(model);
                    model.addColumn("Id");
                    model.addColumn("First Name");
                    model.addColumn("Last Name");
                    model.addColumn("Company Name");
    }
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        model.addRow(new Object[]{jTextField1.getText(), jTextField2.getText(),jTextField3.getText(),jTextField4.getText()});

    }