刷新Jtable [英] Refresh Jtable

查看:91
本文介绍了刷新Jtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从Vector创建的JTable. 如何刷新JTable以显示添加到Vector的新数据?

I have a JTable created from Vector. How can the JTable be refreshed to display new data that is added to the Vector?

推荐答案

当对TableModel进行更改时,您的JTable应该会自动更新.我在这里取得了飞跃,但是我猜想您不是在使用自己的TableModel,而是在Vector中调用了JTable构造函数.在这种情况下,您可以钩住TableModel并将其转换为DefaultTableModel,然后调用其通知方法之一以让JTable知道更改,例如:

Your JTable should update automatically when a change to the TableModel happens. I'm taking a leap here but I'm guessing that you're not using your own TableModel and just called the JTable constructor with your Vector. In this case you can get a hook on the TableModel and cast it to a DefaultTableModel and then call one its notification methods to let the JTable know of a change, something like:

DefaultTableModel model = (DefaultTableModel)table.getModel();
model.fireTableChanged(new TableModelEvent(........));

我会真的建议使用您自己的TableModel,除非这是非常琐碎的事情,但事实是您正在更新数据表明并非如此.

What I would really recommend is using your own TableModel unless this is something very trivial, but the fact you're updating the data indicates it's not.

使用表格,尤其是监听数据更改的部分.

这似乎需要更多的准备工作,但从长远来看,它将为您省去很多头痛,并且是正确的方法

It might seem like more work up front, but it will save you alot of headaches in the long run and is The Right Way to do it

这篇关于刷新Jtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆