现在,我的JTable的Boolean值显示为JCheckBoxes.通常这很好,但是我想将它们显示为替代的String或图像. 我可以让它们显示为true/false,但是如果它们为true,我想将它们显示为选中标记(✔),如果为false,则什么也不显示.可能是图片,但让我们先做一个String ...
Right now my Boolean values for my JTable display as JCheckBoxes. This would normally be fine but I would like to display them as either an alternative String or image. I can get them to display as true/false but I would like to display them as a checkmark (✔) if true and nothing if false. Possibly an image but lets do a String first...
推荐答案创建自定义渲染器.扩展DefaultTableCellRenderer并添加您自己的代码以显示所需的内容.它可以是自定义图标,或者如果对勾"是可打印的字符,则可以仅将渲染器文本设置为适当的字符.
Create a custom renderer. Extend the DefaultTableCellRenderer and add your own code to display whatever you want. It could be a custom Icon or if the "checkmark" is a printable character than you can just set the renderer text to the appropriate character.
阅读JTable API,您将找到Swing教程如何使用表"的链接,该链接将提供有关渲染器的更多信息.
Read the JTable API and you will find a link to the Swing tutorial on "How to Use Tables" which will give more information about renderers.
如果您需要更多帮助,请发布 SSCCE ,以显示创建渲染器时遇到的问题.
If you need more help post your SSCCE showing the problems you are having creating the renderer.
本教程显示了如何为给定的类添加自定义渲染器,但未显示如何为特定列添加自定义渲染器.您将使用:
The tutorial shows how to add a custom renderer for a given class but it doesn't show how to add a custom renderer for a specific column. You would use:
table.getColumnModel().getColumn(...).setCellRenderer(...);更多推荐
为布尔值设置Jtable/Column Renderer
发布评论