可编辑的JTable教程 [英] Editable JTable Tutorial

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

问题描述

是否有任何好的书籍或网站可以创建 JTable ?我想让一列可编辑。我想实际将一个继承的 JCheckBox 组件(我们在这里创建)放到一个表列中,而不是只放置表 JCheckBox in基于它是一个可编辑的布尔字段。



我有 JFC Swing Tutorial第二版书,但我想知道是否还有其他例子我可以看一下在,并学习如何更好地处理表格。这本书似乎只是把java的踪迹放在网上并把它放在书中。



我正在重新阅读这些东西,只是好奇是否有人发现了什么这可能会有所帮助。

解决方案

要使列可编辑,您必须覆盖 isCellEditable TableModel 中的code>方法。如果你继承 AbstractTableModel ,那么创建一个 TableModel 是相当容易的,我建议除了最简单的<$ c c $ c> JTable s。



然而,调整 TableModel 只是其中的一部分你需要做什么要在 JTable 中实际获取自定义组件,您需要设置自定义单元格渲染器。要使用交互式自定义组件,您需要设置自定义单元格编辑器。在某些情况下,使用稍微修改过的默认类版本就足够了。



编辑



如果您已经使用委托轻松完成自定义组件:创建一个实现 TableCellEditor 的新类,并返回 new getCellEditorComponent 方法中的组件实例。此方法的参数包括当前值以及单元格坐标,返回表格的链接以及是否选择了单元格。



TableCellEditor 还有一个方法,当用户提交对单元格内容的更改(您可以验证用户输入并调整模型)或取消编辑时调用该方法。如果您以编程方式中止编辑,请确保在编辑器上调用 stopEditing()方法,否则编辑器组件将保留在屏幕上 - 这曾经花了我2个小时到debug。



请注意,在 JTable 编辑器和编辑器内接收事件!可以使用渲染器显示按钮。但要获得一个正常运行的按钮,您需要使用正确的 EventListeners 注册来实现编辑器。在渲染器上注册侦听器无效。



渲染器



实现渲染器对于您在问题中描述的内容并不是绝对必要的,但无论如何,如果仅进行微小修改,您通常最终会这样做。与编辑不同,渲染器对速度至关重要。 对于表中的每个单元格,一次为渲染器调用 getTableCellRendererComponent 渲染器返回的组件仅用于绘制单元格,而不是用于交互,因此可以重用下一个单元格。换句话说,你应该调整组件(例如使用 setText(...) setFont(...)如果它是渲染器中的 TextComponent ),你应该实例化一个新的 - 这是一种削弱性能的简单方法。 / p>

警告



请注意,要使渲染器和编辑器正常工作,您需要告知 JTable 何时使用某个渲染器/编辑器。基本上有两种方法可以做到这一点。您可以使用相应的 JTable 方法为特定类型设置默认单元格渲染器/编辑器。要使用这种方式, TableModel 需要在 getColumnClass(...)方法中返回此类型!默认表模型将为您执行此操作,它始终返回 Object.class 。我确信那个人已经困扰了很多人。



设置编辑器/渲染器的另一种方法是在列本身上明确设置它,即,通过 JTable的 getTableColumn(...)方法获取 TableColumn 。这是更复杂的,但是,它也是为单个类提供两个不同的渲染器/编辑器的唯一方法。例如。你的模型可能有两列String类,它们以完全不同的方式呈现,可能一次使用 JLabel / DefaultRenderer ,另一列使用 JButton 访问更精细的编辑器。



JTable 及其自定义渲染器和编辑器是<我非常多才多艺,但它也有很多东西可以接受,并且有很多事情要做错。祝你好运!



如何使用表格 Swing Tutorial 是任何定制JTable的人的必读书。特别是,阅读并重新阅读概念:编辑和渲染器因为点击通常需要一段时间。自定义渲染器和编辑器的示例也非常值得。


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.

解决方案

To make a column editable you have to override the isCellEditable method in the TableModel. Creating a TableModel is fairly easy if you inherit AbstractTableModel and I'd recommend it for all but the most simple JTables.

However, adapting the TableModel is only part of what you need to do. To actually get a custom component in the JTable, 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 the getCellEditorComponent 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 the stopEditing() 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 correct EventListeners 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. using setText(...) or setFont(...) if it is a TextComponent) 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 respective JTable methods. For this way to work, your TableModel needs to return exactly this type in the getColumnClass(...) method! The default table model will not do this for you, it always returns Object.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 the getTableColumn(...) method of the JTable. 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 a JLabel/DefaultRenderer and the other using a JButton 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.

这篇关于可编辑的JTable教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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