使用SwingWorker在GUI中添加进度条

编程入门 行业动态 更新时间:2024-10-09 09:20:20
本文介绍了使用SwingWorker在GUI中添加进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用SwingWorker制作带有Java Swing API的进度条。

I use SwingWorker to make a progress bar with Java Swing APIs.

我有一个扩展SwingWorker的类

I have a class that extends SwingWorker

class Swinger extends SwingWorker { private ClassAnalyzer classAnalyzer; public Swinger(ClassAnalyzer classAnalyzer){ this.classAnalyzer = classAnalyzer; } @Override public Void doInBackground() throws InterruptedException { try { int progress = 0; while (progress < 100) { // at this point I make certain elaboration on classAnalyzer progress++; //Call the process method to update the GUI publish(progress); } } catch(InterruptedException e) { } return null; } @Override protected void process(List chunks) { for (Integer chunk : chunks) { progressBar.setValue(chunk); //if the switchtype checkbox is selected then //change the progressbar to a determined type //once the progress has reached 50 if (chunk > 49) { if (switchType.isEnabled() && switchType.isSelected()) { progressBar.setStringPainted(true); } } } } }

和第二类(我正在写一部分)

and a second class (I'm writing a piece of this)

public Tester() { JFrame guiFrame = new JFrame(); //make sure the program exits when the frame closes guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); guiFrame.setTitle("Creating a Table Example"); guiFrame.setSize(700,200); //This will center the JFrame in the middle of the screen guiFrame.setLocationRelativeTo(null); goButton = new JButton("Go"); goButton.setActionCommand("Go"); goButton.addActionListener(new ActionListener() { //When the button is clicked the SwingWorker class is executed and //the button is disabled @Override public void actionPerformed(ActionEvent event) { progressBar.setStringPainted(progressType.isSelected()); ClassAnalyzer c = new ClassAnalyzer(); Swinger task = new Swinger(c); task.execute(); int methods = c.getNumberOfMethods(); if(methods == 0){ JOptionPane.showMessageDialogo(null, "methods not found"); } goButton.setEnabled(false); } }); }

当我在测试仪中启动第二课时,消息在进度条出现之前显示找不到方法,而我希望在之后出现消息。怎么办?

When I launch in a Tester the second class, the message "methods not found" is displayed before progress bar appears, while I would like message appears in case after. What to do?

推荐答案

task.execute()将启动一个background(将从中调用 doInBackground 方法),程序将继续执行。

task.execute() will launch a background (in which the doInBackground method will be called from) and the program will continue executing.

task.execute()不是阻塞方法,这是使用它的原因,所以你不要阻止事件调度线程

task.execute() is not a blocking method, this is the reason for using it, so you don't block the Event Dispatching Thread

您可以使用 PropertyChangeListener

final ClassAnalyzer c = new ClassAnalyzer(); Swinger task = new Swinger(c); task.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals("state") && evt.getNewValue().equals(SwingWorker.StateValue.DONE)) { int methods = c.getNumberOfMethods(); if(methods == 0){ JOptionPane.showMessageDialogo(null, "methods not found"); } } } }); task.execute();

更多推荐

使用SwingWorker在GUI中添加进度条

本文发布于:2023-11-26 09:26:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度条   SwingWorker   GUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!