为布尔值设置Jtable/Column Renderer [英] Set Jtable/Column Renderer for booleans

查看:64
本文介绍了为布尔值设置Jtable/Column Renderer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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