简体   繁体   English

JProgressBar更新

[英]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 ? 我的问题是jprogressbar如何放在单元格表中,更新变量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). JTable的表模型应具有“下载进度”列,其中包含下载百分比值(即0到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 ). 渲染器将​​使用进度条显示表单元格中包含的百分比(即TableCellRenderer的唯一方法的value参数)。

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 ). 然后,此更改将触发TableModelEvent (使用DefaultTableModel自动完成,但是如果要继承AbstractTableModel则必须调用fireTableCellUpdated )。 The event will be "caught" by the JTable which will refresh the value and thus call your renderer with the new value to display. JTable将“捕获”该事件,该JTable将刷新该值,然后使用要显示的新值调用渲染器。

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 . 可能需要包装在SwingUtilities.invokeLater

This is because Swing is thread unsafe and the object of the Swing framework needs to be accessed from a single thread. 这是因为Swing是线程不安全的,并且需要从单个线程访问Swing框架的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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