JTable 不呈现
当加载带有小程序的页面时,JTable 对象的内容将完美呈现。当我关闭选项卡并重新打开它时,会呈现在开始时设置的内容,但是当我通过 setValeAt() 更改的内容表变空时。当我从数据模型中删除所有 html 标签时,一切正常。我对表的默认渲染器不执行任何操作,仅实现数据模型。所有值都是通过 setValeAt() 设置的并且它有效(我检查过)。 有人知道可能出了什么问题吗?
这是重现错误的示例代码。运行小程序(值将发生变化),然后关闭选项卡(不是浏览器)并重新打开它。我在 Linux 和 Windows XP 上的 Firefox 3.6.13 和 Java 1.6.0_22-b04 中测试了它。它的作用是一样的。
import javax.swing.JApplet;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test extends JApplet
{
class model extends AbstractTableModel
{
@Override
public int getColumnCount()
{
return 3;
}
@Override
public int getRowCount()
{
return 3;
}
@Override
public Object getValueAt(int arg0, int arg1)
{
return "<html><b>" + Math.random() + "</b></html>"; // does not work
//return "" + Math.random(); // work
}
public void setValueAt(Object newValue, int row, int col)
{
fireTableCellUpdated(row, col );
}
}
JTable t = new JTable();
public void init()
{
t.setModel( new model() );
add( t );
}
public void start()
{
new Thread( new Runnable()
{
@Override
public void run()
{
int i=0;
try
{
while( ++i<100 )
{
Thread.sleep(100); // thanks to camickr
t.setValueAt(Void.class, (int)(Math.random()*10) %3, (int)(Math.random()*10) %3 );
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}).start();
}
}
编辑:
index.html
内的 HTML 小程序代码
<applet width="500px" height="500px" alt="test test test" code="test.class">
</applet>
编译代码: javac test.java
生成:文件 test$1.class test.class test$model.class
第一次运行正常。创建新选项卡,然后关闭小程序选项卡。重新打开小程序选项卡(不要关闭浏览器)并且表格内容不会呈现。
编辑2
这是一个 JVM 错误!请参阅https://bugs.java.com/bugdatabase/view_bug?bug_id=6995386 和其他相关的。它仅影响 JRE 1.6.0_22
When page with applet is loaded JTable object's content is rendered perfectly. When I close a tab and reopen it content set at start is rendered, but when the content I changed via setValeAt()
table becomes empty. When i remove all html tags from data model everything works. I do nothing with table's default renderer only implent data model. All values are set via setValeAt()
and it works (i checked it).
Can someone have any idea what could be wrong?
Here is sample code wich reproduces the error. Run the applet (values will be changing) and then close the tab (NOT browser) and reopen it. I tested it in Firefox 3.6.13 and Java 1.6.0_22-b04 on Linux and Windows XP. It acts the same.
import javax.swing.JApplet;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class test extends JApplet
{
class model extends AbstractTableModel
{
@Override
public int getColumnCount()
{
return 3;
}
@Override
public int getRowCount()
{
return 3;
}
@Override
public Object getValueAt(int arg0, int arg1)
{
return "<html><b>" + Math.random() + "</b></html>"; // does not work
//return "" + Math.random(); // work
}
public void setValueAt(Object newValue, int row, int col)
{
fireTableCellUpdated(row, col );
}
}
JTable t = new JTable();
public void init()
{
t.setModel( new model() );
add( t );
}
public void start()
{
new Thread( new Runnable()
{
@Override
public void run()
{
int i=0;
try
{
while( ++i<100 )
{
Thread.sleep(100); // thanks to camickr
t.setValueAt(Void.class, (int)(Math.random()*10) %3, (int)(Math.random()*10) %3 );
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}).start();
}
}
EDIT:
HTML applet code inside index.html
<applet width="500px" height="500px" alt="test test test" code="test.class">
</applet>
Compilation code:javac test.java
produces: files test$1.class test.class test$model.class
Running first time OK. Make new tab and then close applet tab. Reopen applet tab (do not close browser) and table content does not render.
EDIT 2
IT IS A JVM BUG! see https://bugs.java.com/bugdatabase/view_bug?bug_id=6995386 and others related. It only affects JRE 1.6.0_22
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法在 XP 上使用 JDK6_7 重现该问题。
值不会不断变化,因为每次更新模型后您都不会睡觉。我稍微改变了代码:
I couldn't reproduce the problem using JDK6_7 on XP.
The values don't keep changing since you don't sleep after each update to the model. I changed the code slightly: