TableCellRenderer 奇怪的行为 java

发布于 2024-12-28 05:19:08 字数 2268 浏览 2 评论 0原文

我正在尝试在 jtable 上实现自定义 TableCellRenderer。该表设置为 100 行和 100 列。该表应包含指定字体的所有字形。我的问题是,当表中的值不完全完整时,它会在第一列上放置最后一个值,直到它到达表的底部。下面是我的自定义渲染器的代码和具有奇怪行为的屏幕截图。任何帮助将不胜感激。

在此处输入图像描述

public class FontRenderer extends JLabel implements TableCellRenderer
{
Font desired_font;
Object prec_value;

public FontRenderer(Font f)
{
    desired_font = f;
}

public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
{


    setOpaque(true);
    setHorizontalAlignment(SwingConstants.CENTER);
    setBackground(new Color(255, 255, 255));
    if (isSelected)
    {
        if (value == null)
        {
            setText("");
        }
        else
        {
            setText(value.toString());
        }
        setFont(desired_font);
        setBackground(new Color(56, 195, 254));
    }
    if (value == null)
    {
        setText("");
    }
    else
    {
        if(value==null)
            table.setValueAt(null, rowIndex, vColIndex);
        else
        setText(value.toString());
            //table.setValueAt(value.toString(), rowIndex, vColIndex);

    }
    setFont(desired_font);

    return this;
}
}

编辑:这是我填充表格的代码。

while (cnt_i < 100) {
    while (cnt_j < 100) {
        if (my_fnt.canDisplay((char) unicode_char) && glyph_count <= total_glyphs) {
            jTable1.setValueAt((char) unicode_char, cnt_i, cnt_j);
            cnt_j++;
            if (glyph_count == total_glyphs) {
                break;
            }
            glyph_count++;
        }
        unicode_char++;
    }
    cnt_i++;
    cnt_j = 0;
}

解决了。全部。这就是我填充表格的方式。以下代码有更改:

while (cnt_i < 100) {
    while (cnt_j < 100) {
        if (my_fnt.canDisplay((char) unicode_char) && glyph_count <= total_glyphs) {
            if (glyph_count == total_glyphs) {
                break;
            }
            else {
                jTable1.setValueAt((char) unicode_char, cnt_i, cnt_j);
                cnt_j++;
                glyph_count++;
            }
        }
        unicode_char++;
    }
    cnt_i++;
    cnt_j = 0;
}

I'm trying to implement a custom TableCellRenderer on a jtable. the table is set to 100 rows and 100 columns. This table should contain all the glyphs for a specified font. My problem is that when the table is not fully complete with values, on the first column it puts the last value until it reaches the bottom of the table. Below i have the code for my custom renderer and a screenshot with the strange behaviour. Any help would be apreciated.

enter image description here

public class FontRenderer extends JLabel implements TableCellRenderer
{
Font desired_font;
Object prec_value;

public FontRenderer(Font f)
{
    desired_font = f;
}

public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
{


    setOpaque(true);
    setHorizontalAlignment(SwingConstants.CENTER);
    setBackground(new Color(255, 255, 255));
    if (isSelected)
    {
        if (value == null)
        {
            setText("");
        }
        else
        {
            setText(value.toString());
        }
        setFont(desired_font);
        setBackground(new Color(56, 195, 254));
    }
    if (value == null)
    {
        setText("");
    }
    else
    {
        if(value==null)
            table.setValueAt(null, rowIndex, vColIndex);
        else
        setText(value.toString());
            //table.setValueAt(value.toString(), rowIndex, vColIndex);

    }
    setFont(desired_font);

    return this;
}
}

Edit: Here is the code where I populate the table.

while (cnt_i < 100) {
    while (cnt_j < 100) {
        if (my_fnt.canDisplay((char) unicode_char) && glyph_count <= total_glyphs) {
            jTable1.setValueAt((char) unicode_char, cnt_i, cnt_j);
            cnt_j++;
            if (glyph_count == total_glyphs) {
                break;
            }
            glyph_count++;
        }
        unicode_char++;
    }
    cnt_i++;
    cnt_j = 0;
}

Solved it. Ty all. it was how i populated the table. the following code has the changes:

while (cnt_i < 100) {
    while (cnt_j < 100) {
        if (my_fnt.canDisplay((char) unicode_char) && glyph_count <= total_glyphs) {
            if (glyph_count == total_glyphs) {
                break;
            }
            else {
                jTable1.setValueAt((char) unicode_char, cnt_i, cnt_j);
                cnt_j++;
                glyph_count++;
            }
        }
        unicode_char++;
    }
    cnt_i++;
    cnt_j = 0;
}

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

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

发布评论

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

评论(3

如日中天 2025-01-04 05:19:08

1)有大约 Unicode 字符,我认为这不是 Renderer 的工作

2)设置 JTable#FontJTable 而不是为 Renderer

3) 如果您想在运行时更改大量数据,请使用 prepareRenderer

4) 最重要的是查看如何填充 JTable 的 数据并定义/设置字体

1) there are about Unicode Chars, I think that is not job for Renderer

2) set JTable#Font for JTable rather that passing parameters for Renderer

3) use prepareRenderer if you want to change bunch of data on Runtime

4) most important would be to see how did you populate JTable's data and define/set for Font(s)

孤独难免 2025-01-04 05:19:08

我不认为你的问题是 CellRenderer ..

但我为你清理了一下

public class FontRenderer extends JLabel implements TableCellRenderer
{
    Font desired_font;
    Object prec_value;

    public FontRenderer(Font f)
    {
        desired_font = f;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
    {
        setOpaque(true);
        setHorizontalAlignment(SwingConstants.CENTER);
        setBackground(new Color(255, 255, 255));
        setFont(desired_font);

        if (value == null)
        {
            setText("");
        }
        else
        {
            setText(value.toString());
        }

        if (isSelected)
        {
            setBackground(new Color(56, 195, 254));
        }

        //what was that for?
        //table.setValueAt(null, rowIndex, vColIndex);

        return this;
    }
}

I dont think that your problem is the CellRenderer ..

But I cleaned it up a bit for you

public class FontRenderer extends JLabel implements TableCellRenderer
{
    Font desired_font;
    Object prec_value;

    public FontRenderer(Font f)
    {
        desired_font = f;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
    {
        setOpaque(true);
        setHorizontalAlignment(SwingConstants.CENTER);
        setBackground(new Color(255, 255, 255));
        setFont(desired_font);

        if (value == null)
        {
            setText("");
        }
        else
        {
            setText(value.toString());
        }

        if (isSelected)
        {
            setBackground(new Color(56, 195, 254));
        }

        //what was that for?
        //table.setValueAt(null, rowIndex, vColIndex);

        return this;
    }
}
半枫 2025-01-04 05:19:08

顺便说一句, canDisplay(int) 可以帮助确定特定代码点是否在给定的Font 中具有字形。 替换字符是一个方便的占位符,GlyphSet 是一个相关示例。

As an aside, canDisplay(int) may help determine if a particular code point has a glyph in a given Font. REPLACEMENT CHARACTER is a convenient placeholder, and GlyphSet is a related example.

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