为布尔值设置Jtable/Column Renderer

编程入门 行业动态 更新时间:2024-10-26 10:40:27
本文介绍了为布尔值设置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

本文发布于:2023-11-05 15:47:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔值   Jtable   Renderer   Column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!