为布尔值设置 Jtable/Column Renderer

发布于 2024-09-02 07:46:07 字数 174 浏览 3 评论 0原文

现在,我的 JTable 的 Boolean 值显示为 JCheckBoxes。这通常没问题,但我想将它们显示为替代字符串或图像。 我可以让它们显示为真/假,但如果为真,我希望将它们显示为复选标记 (✔),如果为假,则不显示任何内容。可能是图像,但让我们先做一个字符串......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┾廆蒐ゝ 2024-09-09 07:46:07

创建自定义渲染器。扩展 DefaultTableCellRenderer 并添加您自己的代码以显示您想要的任何内容。它可以是自定义图标,或者如果“复选标记”是可打印字符,那么您只需将渲染器文本设置为适当的字符即可。

阅读 JTable API,您将找到有关“如何使用表”的 Swing 教程的链接,其中将提供有关渲染器的更多信息。

如果您需要更多帮助,请发布您的 SSCCE,显示您在创建渲染器时遇到的问题。

编辑:

本教程展示了如何为给定类添加自定义渲染器,但没有展示如何为特定列添加自定义渲染器。你会使用:

table.getColumnModel().getColumn(...).setCellRenderer(...);

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.

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.

If you need more help post your SSCCE showing the problems you are having creating the renderer.

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(...);
满意归宿 2024-09-09 07:46:07

示例:

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

使用 BooleanRenderer

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

Example:

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

with BooleanRenderer

public class BooleanRenderer extends JLabel implements TableCellRenderer
{
.....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文