简体   繁体   English

TableCellRenderer Java的异常行为

[英]TableCellRenderer strange behaviour java

I'm trying to implement a custom TableCellRenderer on a jtable. 我正在尝试在jtable上实现自定义TableCellRenderer。 the table is set to 100 rows and 100 columns. 该表设置为100行100列。 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. 任何帮助将不胜感激。

在此处输入图片说明

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;
}

1) there are about Unicode Chars, I think that is not job for Renderer 1)关于Unicode字符,我认为这对Renderer来说不是工作

2) set JTable#Font for JTable rather that passing parameters for Renderer 2)为JTable设置JTable#Font而不是为Renderer传递参数

3) use prepareRenderer if you want to change bunch of data on Runtime 3)如果要在运行时更改数据束,请使用prepareRenderer

4) most important would be to see how did you populate JTable's data and define/set for Font(s) 4)最重要的是查看如何填充JTable's数据并为Font(s)定义/设置

I dont think that your problem is the CellRenderer .. 我不认为您的问题是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;
    }
}

As an aside, canDisplay(int) may help determine if a particular code point has a glyph in a given Font . canDisplay(int)说一句, canDisplay(int)可以帮助确定特定代码点是否在给定Font具有字形。 REPLACEMENT CHARACTER is a convenient placeholder, and GlyphSet is a related example. REPLACEMENT CHARACTER是一个方便的占位符,而GlyphSet是一个相关的示例。

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

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