可编辑 JTable 教程
有没有关于创建 JTable
的好书或网站? 我想让一列可编辑。 我想实际上将继承的 JCheckBox
组件(我们在此处创建的)放入表列之一,而不是仅仅将表放入 JCheckBox
中,因为它是一个可编辑的布尔值字段。
我有 JFC Swing 教程第二版 书,但我只是想知道如果还有其他示例,我可以查看并学习如何更好地处理表格。 这本书似乎只是把网上的java‘踪迹’放到了书中。
不过,我正在重新阅读这些内容,只是好奇是否有人找到了可能有更多帮助的东西。
Are there any good books or website that go over creating a JTable
? I want to make one column editable. I would like to actually put a inherited JCheckBox
component (that we created here) into one of the table columns instead of just having the table put JCheckBox
in based on it being an editable boolean
field.
I have the JFC Swing Tutorial Second Edition book but I just would like to know if there are other examples I could look at and learn how to deal with the tables better. The book seems to just take the java 'trail' online and put it in the book.
I am re-reading the stuff though, just curious if anyone has found something that might help out more.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要使列可编辑,您必须重写
TableModel
中的isCellEditable
方法。 如果您继承了AbstractTableModel
,那么创建TableModel
就相当容易了,并且我建议除最简单的JTable
之外的所有模型都使用它。但是,调整
TableModel
只是您需要做的一部分。 要实际在JTable
中获取自定义组件,您需要设置自定义单元格渲染器。 要使用交互式自定义组件,您需要设置自定义单元格编辑器。 在某些情况下,为此使用默认类的稍微修改版本就足够了。编辑器
如果您已经有了自定义组件,则可以使用委托轻松完成:创建一个实现
TableCellEditor
的新类,并返回该组件的新实例getCellEditorComponent
方法中的组件。 此方法的参数包括当前值以及单元格坐标、返回表格的链接以及单元格是否被选择。TableCellEditor
还有一个方法,当用户提交对单元格内容的更改(您可以在其中验证用户输入并调整模型)或取消编辑时调用该方法。 如果您以编程方式中止编辑,请务必在编辑器上调用stopEditing()
方法,否则编辑器组件将保留在屏幕上 - 这曾经花了我大约 2 个小时来调试。请注意,在
JTable
编辑器中,只有编辑器接收事件! 显示按钮可以使用渲染器来完成。 但要获得正常运行的按钮,您需要实现一个注册了正确的EventListener
的编辑器。 在渲染器上注册侦听器不会执行任何操作。渲染器
对于您在问题中描述的内容来说,实现渲染器并不是绝对必要的,但您通常最终都会这样做,即使只是进行较小的修改。 与编辑器不同,渲染器对速度至关重要。 渲染器的
getTableCellRendererComponent
会为表格中的每个单元格调用一次!渲染器返回的组件仅用于绘制单元格,不用于交互,因此可以被下一个单元格“重用”。 换句话说,您应该调整组件(例如,如果它是TextComponent
,则使用setText(...)
或setFont(...)
>) 在渲染器中,您不应该实例化一个新的渲染器——这是一种削弱性能的简单方法。注意事项
请注意,为了让渲染器和编辑器正常工作,您需要告诉
JTable
何时使用特定的渲染器/编辑器。 基本上有两种方法可以做到这一点。 您可以使用相应的JTable
方法为特定类型设置默认单元格渲染器/编辑器。 为了使这种方式发挥作用,您的TableModel
需要在getColumnClass(...)
方法中准确返回此类型! 默认表模型不会为您执行此操作,它始终返回Object.class
。 我相信这一点已经难倒了很多人。设置编辑器/渲染器的另一种方法是在列本身上显式设置它,即通过以下方法的
getTableColumn(...)
方法获取TableColumn
JTable
。 这要复杂得多,但是,这也是为单个类拥有两个不同渲染器/编辑器的唯一方法。 例如,您的模型可能有两列 String 类,它们以完全不同的方式呈现,一次可能使用 JLabel/DefaultRenderer ,另一次使用 JButton 来访问更详细的内容编辑。JTable
及其自定义渲染器和编辑器极其具有多种功能,但它也有很多需要注意的地方,并且有很多事情可能会出错。 祝你好运!如何在 Swing 教程 是任何自定义 JTables 的人的必读内容。 特别是,阅读并重读概念:编辑器和渲染器< /a> 因为它通常需要一段时间才能“点击”。 关于自定义渲染器和编辑器的示例也非常有价值。
To make a column editable you have to override the
isCellEditable
method in theTableModel
. Creating aTableModel
is fairly easy if you inheritAbstractTableModel
and I'd recommend it for all but the most simpleJTable
s.However, adapting the
TableModel
is only part of what you need to do. To actually get a custom component in theJTable
, you need to set a custom cell renderer. To use an interactive custom component, you need to set a custom cell editor. In some cases, it's enough to use slightly modificated versions of the default classes for this.Editors
If you already have got a custom component is easily done using delegation: Create a new class implementing
TableCellEditor
, and return a new instance of the component in thegetCellEditorComponent
method. The paramaters to this method include the current value as well as the cell coordinates, a link back to the table and wether or not the cell is selected.The
TableCellEditor
also has a method that is called when the user commits a change to the cell contents (where you can validate user input and adjust the model) or cancels an edit. Be sure to call thestopEditing()
method on your editor if you ever programmatically abort editing, otherwise the editor component will remain on screen -- this once took me like 2 hours to debug.Note that within a
JTable
editors and only editors receive events! Displaying a button can be done using a renderer. But to get a functioning button, you need to implement an editor with the correctEventListeners
registered. Registering a listener on a renderer does nothing.Renderers
Implementing a renderer is not strictly necessary for what you describe in your question, but you typically end up doing it anyway, if only for minor modifications. Renderers, unlike editors, are speed critical. The
getTableCellRendererComponent
of a renderer is called once for every cell in the table! The component returned by a renderer is only used to paint the cell, not for interaction, and thus can be "reused" for the next cell. In other words, you should adjust the component (e.g. usingsetText(...)
orsetFont(...)
if it is aTextComponent
) in the renderer, you should not instantiate a new one -- that's an easy way to cripple the performance.Caveats
Note that for renderers and editors to work, you need to tell the
JTable
when to use a certain renderer/editor. There are basically two ways to do this. You can set the default cell renderer/editor for a certain type using the respectiveJTable
methods. For this way to work, yourTableModel
needs to return exactly this type in thegetColumnClass(...)
method! The default table model will not do this for you, it always returnsObject.class
. I'm sure that one has stumped a lot of people.The other way to set the editor/renderer is by explicitly setting it on the column itself, that is, by getting the
TableColumn
via thegetTableColumn(...)
method of theJTable
. This is a lot more elaborate, however, it's also the only way to have two different renderers/editors for a single class. E.g. your model might have two columns of class String which are rendered in entirely different ways, maybe once using aJLabel/DefaultRenderer
and the other using aJButton
to access a more elaborate editor.JTable
with its custom renderers and editors is extremely versatile, but it is also a lot to take in, and there are a lot of things to do wrong. Good luck!How to Use Tables in The Swing Tutorial is mandatory reading for anyone customising JTables. In particular, read and reread Concepts: Editors and Renderers because it typically takes a while for it to "click". The examples on custom renderers and editors are also very worthwhile.
您想要扩展以创建您自己的行为的类是 DefaultTableModel。 这将使您能够定义自己的行为。 可以在 sun 网站。
The class you want to look into extending to create your own behavior is DefaultTableModel. That will allow you to define your own behavior. A decent tutorial can be found on sun's site.
javaobby 中的本教程很容易理解。 您引用的在线 Swing Trail for JTable 表明它已被更新。 您是否浏览了整个内容以获取可能更好(不是越新越好)的信息?
This tutorial from the java lobby is easy to follow. The online Swing trail for JTable that you reference indicates that it has been updated. Did you scan through the whole thing for possible better (isn't newer always better) information?
如果您尝试使用具有 1 列可编辑的简单
JTable
并且您知道列位置,则可以始终使用默认表模型并重载isCellEditable
调用。像这样的东西:
并为复选框创建一个渲染器类
If you are trying to use a simple
JTable
with 1 column editable and you know the column location you could always use default table model and overload theisCellEditable
call.something like this :
And for the check box create a renderer class
一些有用的类是:
Package javax.swing.table
:TableModel
-tablemodel
的接口AbstractTableModel
- 扩展的好类,用于使用自定义数据结构创建您自己的表DefaultTableModel
- 可以处理arrays[]
和Vectors
的默认表格模型要禁用对任何单元格的编辑,您需要覆盖
isCellEditable (int row, int col)
方法Some useful classes are:
Package javax.swing.table
:TableModel
- Interface for atablemodel
AbstractTableModel
- Nice class to extend for creating your own table with custom data structuresDefaultTableModel
- Default table model which can deal witharrays[]
andVectors
To disable editing on any cell you need to override the
isCellEditable(int row, int col)
method在表模型中,您应该覆盖“isCellEditable”和“setValueAt”函数,如下所示。
第 4 列是可编辑单元格的列。
然后,当您双击单元格并输入内容时,
setValueAt() 将被调用并将值保存到 tableModel 的 DO 字段 col4 中。
in your table Model, you should override "isCellEditable" and "setValueAt" functions, like below.
Column 4 is the column for editable cells.
Then when you double click the cell and type something,
setValueAt() will be called and save the value to the tableModel's DO,field col4.