简体   繁体   中英

Editing JTable Cells error after changing table model

I have a list that when clicked deletes removes the table and creates a new one with new values. In this function I setRowCount(0)for the table model, this causes an issue with model.addTableModelListener where when get the selected column it outputs -1 instead of the column.

 public void SetList() {

    list.setModel(dlm);

    ClassesPanel.remove(list);
    dlm.removeAllElements();
    if(Classes != null)
    {
    for(int i = 0; i < Classes.length; i++) {
    dlm.addElement(Classes[i]);
    }

    }else {System.out.print("we null"); }
    //dlm.addElement("none");

    list.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent arg0) {
            try {
                if (!arg0.getValueIsAdjusting()) {
                    if(list.getSelectedValue().toString() != null) {
                    SelectedTab = list.getSelectedValue().toString();
                    }
                    try {
                        /// THIS IS WHERE I CREATE A NEW JTABLE /////
                        csvToArray(SelectedTab);
                        scrollPane_table.remove(table);
                        model.setRowCount(0);
                        table.setModel(model);
                        scrollPane_table.repaint();
                        table.repaint();        
                        SetTable();
                        model.fireTableDataChanged();

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });



}

This code runs fine but the part i have trouble is at the end of setting the values for the new table.

 public void SetTable() {
    table = new JTable(model);

    Object[] columnsName = new Object[columnsNumber1];
    columnsName[0] = "Name";
    for(int i = 1; i < columnsNumber1; i++) {
        if(Assignments[i] != null)
        columnsName[i] = Assignments[i].toString();
    }

    Object[] rowData = new Object[columnsNumber1];
    model.setColumnIdentifiers(columnsName);

    if(Students != null) {
    for(int i = 0; i < StudentList.size(); i++){

        rowData[0] = Students[i];
        for( int j = 1; j < columnsNumber1; j++)
        {
            System.out.println(columnsNumber1+" Number of col");
            rowData[j] = Grades[j-1];
        }
          model.addRow(rowData);
    }
    }

    table.setModel(model);
    scrollPane_table.add(table);
    //This works before selecting another item on the list but not after.
    model.addTableModelListener(new TableModelListener() {

        int selectedRowIndex = table.getEditingRow();;

        public void tableChanged(TableModelEvent e) {
              System.out.println("something changed at "+ table.getSelectedColumn()+table.getColumnName(table.getSelectedColumn()));

          }
        });
}

I don't know why this would error out since i reset the model in the set list function.

Don't create a new JTable.

All you need to do is use:

model.setRowCount(0);

This will remove all the data from the model.

The TableModelListener will still be added to the model, so you don't need to reset that either.

All you need to do in the settable() method is use the addRow(...) method to add the data to the model.

Or if the structure of the table is changing (ie you have different columns), then you would:

  1. create a new TableModel and add it to the table.
  2. add the TableModelListener to the TableModel

That's it, there is still no need to create a new JTable.

scrollPane_table.remove(table);
scrollPane_table.add(table)

Get rid of the above code. Don't ever try to add/remove components from a JScrollPane. The components are added to the JViewport of the JScrollPane. If you have need to replace the component in the viewport then you just use:

scrollPane.setViewportView(...);

Edit:

So you need to create a simple example with a JFrame containing a JTable and a JButton with an ActionListener to change the model of the table.

So the basic code would be something like:

table = new JTable( new DefaultTableModel(3, 5) );
frame.add(new JScrollPane(table), BorderLayout.CENTER);

JButton button = new JButton("Change Model");
frame.add(button, BorderLayout.PAGE_END);
button.addActionListener( new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        table.setModel( new DefaultTableModel(3, 7) );
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM