简体   繁体   English

为布尔值设置Jtable / Column Renderer

[英]Set Jtable/Column Renderer for booleans

Right now my Boolean values for my JTable display as JCheckBoxes . 现在我的JTable的Boolean值显示为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... 可能是一个图像,但让我们先做一个字符串......

Create a custom renderer. 创建自定义渲染器。 Extend the DefaultTableCellRenderer and add your own code to display whatever you want. 扩展DefaultTableCellRenderer并添加您自己的代码以显示您想要的任何内容。 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. 它可以是自定义图标,或者如果“复选标记”是可打印字符,则只需将渲染器文本设置为适当的字符即可。

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. 阅读JTable API,您将找到关于“如何使用表”的Swing教程的链接,该教程将提供有关渲染器的更多信息。

If you need more help post your SSCCE showing the problems you are having creating the renderer. 如果您需要更多帮助,请在SSCCE上显示您正在创建渲染器的问题。

Edit: 编辑:

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(...);

Example: 例:

table.setDefaultRenderer(Boolean.class, new BooleanRenderer(true));

with BooleanRenderer 使用BooleanRenderer

public class BooleanRenderer extends JLabel implements TableCellRenderer
{
.....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM