TableCellRenderer 奇怪的行为 java
我正在尝试在 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1)有大约
Unicode
字符,我认为这不是Renderer
的工作2)设置 JTable#Font 为 JTable 而不是为
Renderer
3) 如果您想在运行时更改大量数据,请使用
prepareRenderer
4) 最重要的是查看如何填充
JTable 的
数据并定义/设置字体
1) there are about
Unicode
Chars, I think that is not job forRenderer
2) set JTable#Font for JTable rather that passing parameters for
Renderer
3) use
prepareRenderer
if you want to change bunch of data on Runtime4) most important would be to see how did you populate
JTable's
data and define/set forFont(s)
我不认为你的问题是 CellRenderer ..
但我为你清理了一下
I dont think that your problem is the CellRenderer ..
But I cleaned it up a bit for you
顺便说一句,
canDisplay(int)
可以帮助确定特定代码点是否在给定的Font
中具有字形。替换字符
是一个方便的占位符,GlyphSet
是一个相关示例。As an aside,
canDisplay(int)
may help determine if a particular code point has a glyph in a givenFont
.REPLACEMENT CHARACTER
is a convenient placeholder, andGlyphSet
is a related example.