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.cla开发者_JAVA百科ss, (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 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6995386 and others related. It only affects JRE 1.6.0_22I couldn't reproduce the problem using JDK6_7 on XP.
values will be changing
The values don't keep changing since you don't sleep after each update to the model. I changed the code slightly:
// Thread.sleep(100);
while( ++i<100 )
{
t.setValueAt(Void.class, (int)(Math.random()*10) %3, (int)(Math.random()*10) %3 );
Thread.sleep(500);
}
精彩评论