为什么 JTables 没有显示添加内容?
我将对象从一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从我昨天给你的工作 TableModel 中复制代码就这么多了。那不是我给你的代码。
我不明白为什么需要检查表模型是否存在。您应该始终创建并显示带有表模型的表,即使该表不包含任何行。
So much for copying the code from the working TableModel I gave you yesterday. That is not the code I gave you.
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.
您应该让表侦听器知道有关更改的信息,例如在 AbstractTableModel 中调用 fireTableRowsInserted() 或 fireTableRowsDeleted()
You should let table listeners know abut the changes see for example calls fireTableRowsInserted() or fireTableRowsDeleted() in AbstractTableModel
如果我正确理解您的代码,那么您每次移动项目时都会设置一个新模型。
添加/删除项目时,您应该保留模型并调用相应的 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.