简体   繁体   English

Java GUI JProgressBar没有绘画

[英]Java GUI JProgressBar not painting

I have a GUI problem that I would like to get sorted out, but I am baffled as to what's happening and hope one of you can explain it. 我有一个GUI问题,我想整理出来,但我对发生的事情感到困惑,希望你们中的一个人能解释一下。 The code base is way too large to upload however I will explain in detail what's happening: 代码库太大而无法上传,但我会详细解释发生了什么:

I have a class ProgessBar which is a JDialog containing a swing JProgressBar . 我有一个ProgessBar类,它是一个包含swing JProgressBarJDialog I have some getters and setters to change the bar to my liking however here comes the issue. 我有一些吸气剂和制定者可以根据自己的喜好改变标准,但问题就出现了。

The ProgressBar is spawned inside of a method myButtonActionPerformed ProgressBar是在myButtonActionPerformed方法中生成的

myButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myButtonActionPerformed(evt);
            }
        });

essentially when the user hits this button, the processing begins and the ProgressBar is spawned. 基本上当用户点击此按钮时,处理开始并生成ProgressBar

The user currently has a JFrame in front of them and this progress bar is popped up in a JDialog . 用户当前在它们前面有一个JFrame ,这个进度条在JDialog弹出。 After stepping through debug mode in Netbeans, I can see the JProgressBar 's values getting changed, but the bar visually stays at 0% while my program is processing then instantly jumps to 100% the moment it leaves the add action listener method above, almost as if it was waiting to repaint until out of that listener. 在Netbeans中逐步调试模式之后,我可以看到JProgressBar的值发生了变化,但是当我的程序处理时,条形图在视觉上保持0%,然后在离开上面的添加动作侦听器方法时立即跳转到100%,几乎就好像等待重新画画一样,直到那个听众。 What am I not understanding? 我不明白的是什么? Is there something I can call that will force it to update inside of this myButtonActionPerformed method instead of waiting until it's useless. 有什么我可以调用它会强制它在这个myButtonActionPerformed方法内更新,而不是等到它没用。

Are you doing whatever takes that time in the EDT? 你在做EDT的那段时间吗? Keep in mind that AWT/Swing have a dedicated thread that does GUI work – handling event handlers, repainting the GUI, etc. If you do long-running things on that thread, Swing will not repaint. 请记住,AWT / Swing有一个专门的线程来完成GUI的工作 - 处理事件处理程序,重新绘制GUI等。如果你在该线程上做长时间运行,Swing将不会重新绘制。

Try performing your task in another thread and update the progressbar from there accordingly. 尝试在另一个线程中执行您的任务并相应地更新进度条。 Use SwingUtilities.invokeLater or invokeAndWait to update the progressbar, though, to ensure that the GUI updates happen on the EDT. 但是,使用SwingUtilities.invokeLaterinvokeAndWait更新进度条,以确保在EDT上进行GUI更新。 Otherwise things get very weird. 否则事情变得非常奇怪。

The reason it doesn't update is because your UI thread is busy in the ActionPerformed listener doing processing. 它不更新的原因是因为您的UI线程忙于处理ActionPerformed监听器。 If you try and interact with your application, you'll notice that nothing on the UI is responsive. 如果您尝试与应用程序进行交互,您会注意到UI上的任何内容都没有响应。

The solution to this is to not do processing on the UI thread - launch another thread, do the processing on that, and call back to the UI thread to update the progress bar. 解决方法是不对UI线程进行处理 - 启动另一个线程,对其进行处理,然后回调UI线程以更新进度条。

First your anonymous ActionListener simply forwards to a method called myButtonActionPerformed, so obviously the issue is not in the posted code, but rather in something that happens or is caused to happen inside myButtonActionPerformed. 首先你的匿名ActionListener只是转发到一个名为myButtonActionPerformed的方法,所以显然问题不在于发布的代码中,而是在myButtonActionPerformed中发生或导致发生的事情。 So that would be the relevant code to post (if possible). 这就是要发布的相关代码(如果可能的话)。

Second other people probably nailed this already because the likely thing is indeed heavy processing on the EDT. 第二个人可能已经把这个问题钉在了一起,因为可能的事情确实是EDT上的繁重处理。 One can use SwingWorkers to efficiently route the progress/status updates to the GUI thread as appropriate and also move the background processing off the EDT. 可以使用SwingWorkers根据需要有效地将进度/状态更新路由到GUI线程,并将后台处理移出EDT。

Third: out of interest, do you happen to use NetBeans for your GUI design? 第三:出于兴趣,您是否碰巧使用NetBeans进行GUI设计? Looks like its automatically generated code, to me. 看起来像它自动生成的代码,对我来说。

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

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