将多个JProgressBar添加到JTable的TableColumn中
我已将多个 JProgressBar
添加到 JTable
的 TableColumn
中。
我在进行某些计算后用数据更新所有 JProgressBar,但仅添加最后一个 ProgressBar
(在本例中为 ProgressBar
progressObj4
)正在更新。
如何更新所有 ProgressBar
?
基本要求是我在上传时在进度栏中显示文件的状态。目前,我正在对 4 个进度条进行硬编码,以测试所有进度条是否都根据文件的状态进行更新,但我需要动态创建它们。进度条总数与正在上传的文件数有关。另外,我如何获取进度条和进度条的各个实例?更新他们的状态?
我附加了添加到表列的进度条的源代码。
//tc = object of TableColumn
progressObj1 = new ProgressBarRenderer("Progress1");
progressObj1.setValue(0);
progressObj1.setStringPainted(true);
progressObj1.setBackground(Color.WHITE);
progressObj1.setBorderPainted(true);
tc.setCellRenderer(progressObj1);
progressObj2 = new ProgressBarRenderer("Progress2");
progressObj2.setValue(0);
progressObj2.setStringPainted(true);
progressObj2.setBackground(Color.WHITE);
progressObj2.setBorderPainted(true);
tc.setCellRenderer(progressObj2);
progressObj3 = new ProgressBarRenderer("Progress3");
progressObj3.setValue(0);
progressObj3.setStringPainted(true);
progressObj3.setBackground(Color.WHITE);
progressObj3.setBorderPainted(true);
tc.setCellRenderer(progressObj3);
progressObj4 = new ProgressBarRenderer("Progress4");
progressObj4.setValue(0);
progressObj4.setStringPainted(true);
progressObj4.setBackground(Color.WHITE);
progressObj4.setBorderPainted(true);
tc.setCellRenderer(progressObj4);
I have added multiple JProgressBar
to TableColumn
of JTable
.
I am updating all the JProgressBar with data after making certain calculations, but only the last ProgressBar
(in this case ProgressBar
progressObj4
) which is added is getting updated.
How can I update all the ProgressBar
s?
The basic requirement is that I am displaying the status of file in progress bar while uploading . Currently I am hardcoding 4 progress bars to test if all the progress bars are getting update wrt the status of the file, but I need to create them dynamically. The total no of progress bars wrt the no of files which is getting uploaded. Also, how can I fetch the individual instances of the progress bars & update their status ?
I am attaching the source code of the progressbar getting added to the table column.
//tc = object of TableColumn
progressObj1 = new ProgressBarRenderer("Progress1");
progressObj1.setValue(0);
progressObj1.setStringPainted(true);
progressObj1.setBackground(Color.WHITE);
progressObj1.setBorderPainted(true);
tc.setCellRenderer(progressObj1);
progressObj2 = new ProgressBarRenderer("Progress2");
progressObj2.setValue(0);
progressObj2.setStringPainted(true);
progressObj2.setBackground(Color.WHITE);
progressObj2.setBorderPainted(true);
tc.setCellRenderer(progressObj2);
progressObj3 = new ProgressBarRenderer("Progress3");
progressObj3.setValue(0);
progressObj3.setStringPainted(true);
progressObj3.setBackground(Color.WHITE);
progressObj3.setBorderPainted(true);
tc.setCellRenderer(progressObj3);
progressObj4 = new ProgressBarRenderer("Progress4");
progressObj4.setValue(0);
progressObj4.setStringPainted(true);
progressObj4.setBackground(Color.WHITE);
progressObj4.setBorderPainted(true);
tc.setCellRenderer(progressObj4);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上有两种方法通过使用 SwingWorker 来移动
JProgressBar
和 Runnable#Thread,SwingWorker 的示例basically there are two ways move with
JProgressBar
by using SwingWorker and Runnable#Thread, example forSwingWorker
据我所知,渲染器适用于给定列中的所有行。在我看来,您想将多个渲染器应用到同一列。此外,您似乎还试图提供单元格渲染器状态。
我相信这将有助于使渲染器成为无状态,并让渲染器的方法 getTableCellRendererComponent() 在渲染器绘制单元格之前根据当前行的值来设置各种 (JProgressBar) 属性。
换句话说,您只需要为任何给定列调用 tc.setCellRenderer() 一次,然后让单元格渲染器为任何给定行绘制列(例如,基于该表的基础数据模型)。
To my knowledge, a renderer applies to all rows in a given column. It seems to me that you would like to apply several renderers to the same column. Furthermore, it also seems that you are attempting to give the cell renderer state.
I believe it will help to make the renderer stateless and have the renderer's method getTableCellRendererComponent() take care of setting the various (JProgressBar) properties based on values from the current row before the renderer paints the cell.
In other words, you should only need to invoke tc.setCellRenderer() once for any given column, and then have your cell renderer to draw the column for any given row (based on, say, the underlying data model for that table).