为什么 JTables 没有显示添加内容?

发布于 2024-10-20 22:30:38 字数 1996 浏览 6 评论 0原文

我将对象从一个 JTable 添加到另一个 JTable,并且通过调试可以看到在 CustomTableModel 中对象被添加到对象列表中。只有我添加的第一个对象显示在新的 JTable 中。

因此,我可以向 TableModel 添加许多对象,但只有第一个对象显示在 JTable 中。

这是我的添加方法:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        // TODO add your handling code here:
       if(physPackageModel != null){
        int h = secRowSeat2.getSelectedRow();
        Physical_Package pp = PpackageList.get(h);
           if(physPackageModel2 != null){
                 physPackageModel2.addRow(pp);
                 physPackageModel.removeRow(h);

           }
           else{

                    physPackageModel2 = new tableModel2();
                    physPackageModel2.addRow(pp);
                    physPackageModel.removeRow(h);



           }
        secRowSeat1.setModel(physPackageModel2);
        }
       else{
          int h = secRowSeat2.getSelectedRow();
          EventSeat es = eventSeatList.get(h);

        if(eventSeatModel2 != null){
                eventSeatModel2.addRow(es);
                eventSeatModel.removeRow(h);
        }else{

                eventSeatModel2 = new EventTableModel2();
                eventSeatModel2.addRow(es);
                eventSeatModel.removeRow(h);
           }
        secRowSeat1.setModel(eventSeatModel2);
        secRowSeat2.setModel(eventSeatModel);
        repaint();
       }

    }

如果您想查看我的自定义表模型......请告诉我

从 customTableModel 添加和删除方法:

public void addRow(Physical_Package rowData)
    {
        insertRow(getRowCount(), rowData);
    }

    public void insertRow(int row, Physical_Package rowData)
    {
        modelData.add(row-1, rowData);
        fireTableRowsInserted(row, row);
                this.fireTableDataChanged();
    }
    public void removeRow(int row)
    {
        modelData.remove(row);
        fireTableRowsDeleted(row, row);

    }

I am adding objects from one JTable to another, and I can see through debugging that in the CustomTableModel the objects are being added to the list of objects. Only the first object I add shows up in the new JTable.

So I can add many objects to the TableModel, but only the first one is showing up in the JTable.

here's my add method:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        // TODO add your handling code here:
       if(physPackageModel != null){
        int h = secRowSeat2.getSelectedRow();
        Physical_Package pp = PpackageList.get(h);
           if(physPackageModel2 != null){
                 physPackageModel2.addRow(pp);
                 physPackageModel.removeRow(h);

           }
           else{

                    physPackageModel2 = new tableModel2();
                    physPackageModel2.addRow(pp);
                    physPackageModel.removeRow(h);



           }
        secRowSeat1.setModel(physPackageModel2);
        }
       else{
          int h = secRowSeat2.getSelectedRow();
          EventSeat es = eventSeatList.get(h);

        if(eventSeatModel2 != null){
                eventSeatModel2.addRow(es);
                eventSeatModel.removeRow(h);
        }else{

                eventSeatModel2 = new EventTableModel2();
                eventSeatModel2.addRow(es);
                eventSeatModel.removeRow(h);
           }
        secRowSeat1.setModel(eventSeatModel2);
        secRowSeat2.setModel(eventSeatModel);
        repaint();
       }

    }

Let me know if you want to see my custom table model's.....

add and remove methods from customTableModel:

public void addRow(Physical_Package rowData)
    {
        insertRow(getRowCount(), rowData);
    }

    public void insertRow(int row, Physical_Package rowData)
    {
        modelData.add(row-1, rowData);
        fireTableRowsInserted(row, row);
                this.fireTableDataChanged();
    }
    public void removeRow(int row)
    {
        modelData.remove(row);
        fireTableRowsDeleted(row, row);

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天生の放荡 2024-10-27 22:30:38
public void insertRow(int row, Physical_Package rowData)     
{
         modelData.add(row-1, rowData);
         fireTableRowsInserted(row, row);
         this.fireTableDataChanged();
} 

从我昨天给你的工作 TableModel 中复制代码就这么多了。那不是我给你的代码。

  1. 我没有使用 fireTableDataChanged
  2. 我没有在 add(...) 方法中使用“row - 1”。当然,如果您在位置 0 添加一行,然后告诉表已更新您插入了第 1 行,则 fireTableRowsInserted(...) 方法将不起作用,该表将尝试重新绘制第 1 行。

我不明白为什么需要检查表模型是否存在。您应该始终创建并显示带有表模型的表,即使该表不包含任何行。

public void insertRow(int row, Physical_Package rowData)     
{
         modelData.add(row-1, rowData);
         fireTableRowsInserted(row, row);
         this.fireTableDataChanged();
} 

So much for copying the code from the working TableModel I gave you yesterday. That is not the code I gave you.

  1. I did not use fireTableDataChanged
  2. I did not use "row - 1" in the add(...) method. Of course the fireTableRowsInserted(...) method won't work if you add a row at postion 0, and then tell the table updated you inserted row 1, The table will attempt to repaint row 1.

I don't see why you need any check for the existence of a table model. You should always create and display a table with a table model, even if the table contains no rows.

对不⑦ 2024-10-27 22:30:38

您应该让表侦听器知道有关更改的信息,例如在 AbstractTableModel 中调用 fireTableRowsInserted() 或 fireTableRowsDeleted()

You should let table listeners know abut the changes see for example calls fireTableRowsInserted() or fireTableRowsDeleted() in AbstractTableModel

乖乖兔^ω^ 2024-10-27 22:30:38

如果我正确理解您的代码,那么您每次移动项目时都会设置一个新模型。
添加/删除项目时,您应该保留模型并调用相应的 fireXxx() 方法。

If I understand your code correctly, you're setting a new model each time you move an item.
You should keep the model and call the corresponding fireXxx() methods when adding/removing items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文