1

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 ?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
pneumatic
  • 49
  • 1
  • 1
    duplicate: http://stackoverflow.com/questions/7239410/jprogressbar-in-jtable-problem - please don't start over if you dont understand the answer ;-) You'r wasting everybody's time, yours included - sit down and learn the concepts of how a JTable works ... – kleopatra Aug 31 '11 at 10:31
  • @kleopatra forums are places where people help themselves, these tutorials in the net don't contain anserws to my problem. If you dont help better dont say nothing and dont teach me what should I do. – pneumatic Aug 31 '11 at 13:07
  • 1
    actually, they do (if you combined them with the answers you got in your first thread). You are right about the helping - the emphasis is on "themselves" which includes effort on your part as well. – kleopatra Aug 31 '11 at 14:04

2 Answers2

3

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    +1 There's a good example [here](http://stackoverflow.com/questions/7036036/adding-multiple-jprogressbar-to-tablecolumn-of-jtable/7036206#7036206). – trashgod Aug 31 '11 at 10:57
2

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.

aioobe
  • 413,195
  • 112
  • 811
  • 826