简体   繁体   English

从不同的类更新JProgressBar

[英]Update JProgressBar From Different Class

I have built a small java application, it is for updating product price, quantity and special offers etc of products on a mysql database, using a csv file as input that I receive from a closed computer system in my fathers electrical retail business. 我建立了一个小型的Java应用程序,它用于更新mysql数据库上产品的价格,数量和特价等,使用csv文件作为我从父亲电气零售业务中的封闭计算机系统收到的输入。

I have everything working, apart from a progress bar. 除了进度条之外,我一切正常。 I cannot for the life of me work out how to put a JProgressBar into my application because of its setup. 由于设置原因,我无法终生解决如何将JProgressBar放入应用程序中的问题。 I am relatively new to programming, in my first 6 months of a software engineering degree. 在软件工程学位的前六个月中,我对编程还比较陌生。

Basically, I have 3 classes: database (performs all interaction with mysql db) updater (interprets .csv file and interacts with database module to perform functions. It loops through array created from .csv file, and updates records) GUI (A basic gui, consisting of a JFrame, JPanel, buttons and text fields for database login and browsing for .csv file) 基本上,我有3个类:数据库(执行与mysql db的所有交互)更新程序(解释.csv文件并与数据库模块进行交互以执行功能。它遍历从.csv文件创建的数组,并更新记录)GUI(基本gui ,由JFrame,JPanel,按钮和文本字段组成,用于数据库登录和浏览.csv文件)

GUI creates instance of database, using its text fields as input, this is passed to and instance of updater class, also created by GUI. GUI使用其文本字段作为输入来创建数据库实例,该实例将传递给也由GUI创建的updater类的实例。

Where in this project should I insert a progress bar, or what should i change about the setup of my project to allow this? 我应该在该项目中的哪个位置插入进度条,或者应该对此项目的设置进行哪些更改?

Your help will be greatly appreciated, im stuck and have searched the internet and cannot find a suitable way of making the progress bar functional for my situation. 您的帮助将不胜感激,被困在互联网上,无法找到使进度条适合我的情况的合适方法。

And please go easy on me :PI know I have probably made fundamental errors in the setup of the whole thing lol 而且请对我轻松一点:PI知道我在整个事情的设置中可能犯了基本错误,哈哈

Thanks, Wilson. 谢谢,威尔逊。

Progress bar is surely GUI component, so where else it could belong? 进度栏肯定是GUI组件,那么它可能还属于什么地方? However it often shows the progress of something that is happening in your own, non Swing thread (doing too much in Swing thread freezes GUI controls). 但是,它通常显示您自己的非Swing线程中正在发生的事情的进度(在Swing线程中执行过多操作会冻结GUI控件)。 You cannot touch the progress bar from the non Swing thread directly. 您不能直接通过非Swing线程触摸进度条。 Use invokeLater or invokeAndWait to move it forward. 使用invokeLaterinvokeAndWait将其向前移动。 To avoid memory trashing, it is possible to reuse the same Runnable , assigning the wanted progress to the inner field. 为了避免内存浪费,可以重用相同的Runnable ,将所需的进度分配给内部字段。

Runnable doProgress = new Runnable() {

   int progress = 0;

   public void run() {
       myProgressBar.setValue(progress++);
   }
};
// do something 
SwingUtilities.invokeLater(doProgress);
// do more
SwingUtilities.invokeLater(doProgress);

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

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