使用数据和 JTable?
我有一个 JTable,我想用它来显示一些数据(每行中有一个 String
和一个 Boolean
)。 数据由我自己的班级维护。 有没有某种方法将数据模型绑定到 JTable,以便当我添加到模型时,JTable 会动态更新,当我从模型中删除某些内容时,该行将从 JTable 中删除?
我以前使用过 Flex 和 Actionscript,这在数据绑定中很容易做到,所以我只是想知道它是如何在 Java 中完成的。
谢谢。
I have a JTable that I want to use to display some data (a String
and a Boolean
in each row). The data is maintained by my own class. Is there some way to bind the data model to the JTable, so that when I add to the model, the JTable is dynamically updated and when I remove something from the model, the row is removed from the JTable?
I have previously worked with Flex and Actionscript, and this is very easy to do there with data binding, so I'm just wondering how it's done in Java.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要让数据集实现 <代码>TableModel接口。 如果您这样做,那么您可以将其应用到 JTable。 如果您扩展
AbstractTableModel
您将继承表将处理的一些事件触发方法并更新视图。 请参阅本教程。 请注意,JTable
的默认实现将为您呈现数据,如果找到布尔值,它将显示为复选框。You will need to have your dataset implement the
TableModel
interface. if you do that then you can apply it to the JTable. If you extendAbstractTableModel
you will inherit some event firing methods that your table will handle and will update the view. see this tutorial. Note that the default implementation ofJTable
will renderer your data for you, and if a Boolean is found, it will show up as a check box.您可能会找到 Java JTable 教程 JTable API 文档很有帮助了解 JTable 的工作原理,但除此之外,这里有一个快速概述。
JTable 的前提是它与实现
TableModel
接口的对象配对,默认情况下该对象是DefaultTableModel
的实例。 表模型对象由列列表组成,每列都有自己的数据类型(在您的情况下为String
和Boolean
),以及包含以下内容的行列表:表的实际数据。每当 Swing 绘图代码绘制 JTable 时,它都会重复调用该方法:
因此,当您将数据添加到表模型时,它始终会在下一次屏幕刷新时按照您的预期呈现(动态)。
那么,您真正需要担心的唯一一件事就是将数据从对象获取到表模型中并再次返回。 除此之外,JTable 负责处理所有繁重的工作。
You'll probably find both the Java JTable tutorial and the JTable API documentation helpful in understanding how JTable works, but otherwise here's a quick rundown.
The premise of a JTable is that it is paired with an object that implements the
TableModel
interface, which by default is an instance ofDefaultTableModel
. The table model object is made up of a list of columns, each of which has its own data type (String
andBoolean
in your case), and a list of rows containing the actual data for the table.Whenever the JTable is drawn by the swing drawing code, it repeatedly calls the method:
Thus, when you add data to the table model, it is always rendered as you expect in the next screen refresh (dynamically).
The only thing you really need to worry about, then, is getting the data from your object into the table model and back out again. Other than that, JTable takes care off all the heavy lifting.
虽然对于简单的情况来说,实现 TableModel 很容易,但您可能需要考虑真正的绑定方法(我最喜欢的是 Glazed Lists - 观看 30 秒的视频,了解这有多么简单,您将会被说服)。 Beans Binding(现在Better Beans Binding)也有一个可观察列表的实现,可能是有用(尽管我更喜欢 Glazed Lists 方法)
While implementing TableModel is easy enough for simple cases, you might want to consider a true binding approach (my favorite is Glazed Lists - watch the 30 second video on how easy this is and you'll be won over). Beans Binding (now Better Beans Binding) also has an implementation of observable lists that might be useful (although I much prefer the Glazed Lists approach)