简体   繁体   中英

JProgressBar update

Could someone help me ? I would be grateful. I've got example 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 (ie 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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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