JProgressBar不会在循环内实时更新

时间:2016-02-12 12:30:30

标签: java multithreading swing event-dispatch-thread jprogressbar

这是我第一次使用进度条而且我遇到了一个问题,除了我试着从它保持0%的所有地方调用它setValue(x)之后直接调到100%方法例程完成。

我尝试创建一个扩展Thread的内部类,然后在我尝试在我的“main”方法中启动一个新的Thread之后,最后我尝试使用Observer。这些似乎已根据这篇文章发挥作用,但不幸的是没有给我

Update JProgressBar from new Thread

Problem making a JProgressBar update values in Loop (Threaded)

请问,有人可以帮助我吗?

public class MainClass {      

private void checkFiles() {

    Task task = new Task();
    task.start();

    //here I have some Files validation...I don't think it is important to solve the progressbar problem 
    //so it will be ommited


    //in this point I tried to call update to test the observer solution I found in another post here
    //task.update(null, null);

    JOptionPane.showMessageDialog(this, "Done!");
    //here the bar jumps from 0% to 100%

    }


    private class Task extends Thread implements Observer {

    public Task() {
    }

    //Dont bother with the calculum as I haven't finished working on them....
    //The relevant thing here is that it starts a new Thread and I can see the progress
    //increasing on console using system.out but my progress bar still don't change from 0%.
    public void run() {
        int maxSize = 100;
        final int partsSize = maxSize / listaArquivosSelecionados.size();
        while (listFilesValidated.size() != listFilesToValidate.size()) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            int progress = listFilesValidated.size() * partsSize;
            System.out.println("Progress" + progress);
            progressBar.setValue(progress);

            }
        });
        try {
            Thread.sleep(100);
        }
        catch (InterruptedException e) {}
        }
    }

    //Just tried to set any value to check if it would update before the files validation thread finishes its work.
    @Override
    public void update(Observable arg0, Object arg1) {
        progressBar.setValue(66);
    }
}

1 个答案:

答案 0 :(得分:3)

您可以创建另一个ProgressBar类(请参阅Oracle tutorial)并使用它:

SwingWorker worker = new SwingWorker<MyReturnType, Void>() {
    @Override
    public MyReturnType doInBackground() {
        // do your calculation and return the result. Change MyReturnType to whatever you need
    }
    @Override
    public void done() {
        // do stuff you want to do after calculation is done
    }
};

或者您可以使用SwingWorker,例如:

public class MainFirstActivity extends AppCompatActivity {

private Button createFile;
private Button settings;
private boolean start = true;

private static final String MY_SETTINGS = "my_settings";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_first);

    createFile = (Button) findViewById(R.id.create_file);
    settings = (Button) findViewById(R.id.settings);



    createFile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainFirstActivity.this, CreateFileActivity.class);
            startActivity(intent);
           // MainFirstActivity.this.finish();
        }
    });

    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainFirstActivity.this, SettingActivity.class);
            startActivity(intent);
        }
    });
}

@Override
protected void onResume() {
    super.onResume();

    File f = new File(CreateFileActivity.FILE_NAME);
    if(!f.exists() && !f.isDirectory() && start) {
        Intent intent = new Intent(MainFirstActivity.this, MainWithFileActivity.class);
        startActivity(intent);
        //start=false;
        MainFirstActivity.this.finish();
        Log.e("err", "intent");
    }
}
}

几年前我有the same question