JProgressBar update
Could someone help me ? I would be grateful. I've got e开发者_高级运维xample code:
....
int sizeFile;
RandomAccessFile raf;
InputStream in;
int val= 0;
int downloaded= 0;
while((val=in.read(buff)) != -1)
{
raf.write(buff, 0, val);
downloaded+= val;
float wartosc = ((float) downloaded/ sizeFile) * 100;
prog.setValue((int)wartosc);
}
My question is how jprogressbar put in cell table, update variable wartosc ?
The table model of your JTable should have a column "download progress", holding the download percentage value (i.e. a number between 0 and 100).
You should associate a custom table cell renderer to this column. The renderer would use a progress bar to display the percentage contained in the table cell (i.e. the value
argument of the unique method of TableCellRenderer
).
To update the progress bar, you should set a new value for the appropriate cell in the table model. This change will then fire a TableModelEvent
(it's done automatically with a DefaultTableModel
, but you have to call fireTableCellUpdated
if you're subclassing AbstractTableModel
). The event will be "caught" by the JTable which will refresh the value and thus call your renderer with the new value to display.
Read the swing tutorial about tables.
Not entirely sure I understand your question, but here's something to start with...
Assuming you're not doing downloads on the dispatch thread (which would be a bad idea) the following call:
prog.setValue((int) wartosc);
probably needs to be wrapped in a SwingUtilities.invokeLater
.
This is because Swing is thread unsafe and the object of the Swing framework needs to be accessed from a single thread.
精彩评论