简体   繁体   English

JTable数据刷新问题

[英]JTable Data Refresh Issue

I want to refresh the JTable data by clicking a button. 我想通过单击按钮刷新JTable数据。

The problem is that the old data in the JTable can't be removed and the new data are just added into the table. 问题是JTable中的旧数据无法删除,新数据只是添加到表中。 I tried below ways to remove the old data but none of them works. 我尝试了以下方法来删除旧数据,但没有一个工作。

1. table.setModel(new DefaultTableModel());
2. ((DefaultTableModel)table.getModel()).setRowCount(0);
3. ((DefaultTableModel)table.getModel()).fireTableDataChanged();
4. ((DefaultTableModel)table.getModel()).getDataVector().removeAllElements();
5. table.repaint();
6. model = (DefaultTableModel)table.getModel();
   while(model.getRowCount() > 0) {
       model.removeRow(0);
   }

Having a refresh button for a JTable is very suspect. 拥有JTable的刷新按钮是非常可疑的。 It makes me think you aren't correctly adding data as JTables should refresh everytime data is added or removed. 这让我觉得你没有正确添加数据,因为每次添加或删除数据时,JTable都应刷新。

I would verify a couple of things when using a DefaultTableModel: 使用DefaultTableModel时,我会验证一些事情:

  1. Make sure to only add data using addRow 确保仅使用addRow添加数据
  2. Data should only be inserted using insertRow 只应使用insertRow插入数据
  3. Remove data using removeRow 使用removeRow删除数据

Never modify the internal vectors directly. 切勿直接修改内部向量。 It won't cause events to fire and you're stuck with a refresh button. 它不会导致事件触发,而且你会遇到刷新按钮。 I don't know why they even expose it. 我不知道为什么他们甚至暴露它。 The JavaDocs should at least specifically warn against this. JavaDocs至少应该特别警告这一点。

If all else fails, fire up a debugger and see what happens. 如果所有其他方法都失败了,请启动调试器,看看会发生什么。

More of your code might be appropriate here. 您可以在此处使用更多代码。 Hard to tell exactly where you're calling these methods and the order. 很难确切地告诉你在哪里调用这些方法和顺序。 If you change the model and then call fireTableDataChanged() it should work....assuming you've updated the right TableModel. 如果您更改模型然后调用fireTableDataChanged()它应该工作....假设您已更新正确的TableModel。 There is a good Java tutorial for using tables: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html 有一个很好的Java教程来使用表: http//docs.oracle.com/javase/tutorial/uiswing/components/table.html

Had the same issue myself, but my solution was different. 我自己也有同样的问题,但我的解决方案却有所不同。 After checking just about everything, I checked the contents of the table via the console, and found that the contents were indeed being updated. 检查几乎所有内容后,我通过控制台检查了表的内容,发现内容确实正在更新。 However, the update was not being reflected on the table which was visible on the screen. 但是,更新未反映在屏幕上可见的表格上。

In fact, this code: 实际上,这段代码:

model = (DefaultTableModel)table.getModel();


while(model.getRowCount() > 0) {
       model.removeRow(0);
   }

Did not only remove the rows, but also removed the table. 不仅删除了行,还删除了表。

My solution was to remove the table from the form and then re-add it, whenever the table data was changed. 我的解决方案是从表单中删除表,然后在表数据更改时重新添加它。

Seemed in my case there was nothing wrong with my coding to generate the table but that the layout manager didn't like overwriting or updating a component the area where I wanted to put it already had something in there. 在我的情况下看来,我的编码生成表没有任何问题,但布局管理器不喜欢覆盖或更新组件,我希望它放在那里的区域。

Something weird going on methinks but at the end of the day this worked for me. 一些奇怪的事情发生了,但在一天结束时,这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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