SwingWorker - 传递参数并返回ArrayList <object> </object>

时间:2013-10-23 14:43:30

标签: java swing swingworker

注意:此问题末尾的SSCCE

我是SwingWorker的新手,所以请耐心等待。

我正在使用GUI,当用户按下按钮时,它会执行我在另一个类中的一些方法。这个类里面只有方法,我需要这些方法在自己的线程上运行。问题是,这些方法需要传递参数,它们也会返回其他方法所需的东西。

这样的事情:

public class MyClass {

   public  ArrayList<Train> proccessTrain(int endTime, int startTime, ArrayList<Train> trainList)
   {
       (... I start processing data here... with sets and gets to the stationDataList array...)

       return trainList;
   }

   public int getNumberOfCrashes(ArrayList<Train> trainList)
   {
       (... I run over the array... do some comparasions... and do some sets on the Train object in the array...)

       return numOfCrashes;
   }

}

我的GUI类:

public class MyGUI{

   MyClass myClass = new MyClass();
   ArrayList<Train> trainList = new ArrayList<Train>();

   (later on there are Train ojects added into the array)

   (when button is pressed)

       trainList = myClass.proccessTrain(20, 10, trainList);
       int numberOfCrashes = myClass.getNumberOfCrashes(trainList);
}

我还需要更新GUI,这对我来说不是问题因为我知道我可以使用publish()并覆盖swing工作者中的process()方法。 我的问题是如何将方法返回到GUI中的变量,如何将参数传递给MyClass.class中的方法,因为doInBackground()不接受参数。

另外,我需要swingworker在方法内,而不是MyGUI.class,所以像这样(例如processTrain方法):

public  ArrayList<Train> proccessTrain(int endTime, int startTime, ArrayList<Train> trainList)
{
    SwingWorker<String, Integer> worker = new SwingWorker<String, String>() {
        @Override
        protected String doInBackground() throws Exception {

            return 
        }

        protected void done() {

        }

        @Override
        protected void process(List<Integer> chunks) {

        }
    };

    worker.execute();

如果有遗漏和/或不清楚的地方请告诉我,我会更新问题。

//////////////////////////// _ __ EDIT_ _ __ ///////////////////////////////

这里要求的是一个简短的可执行代码:

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MainFrame extends JFrame {

private JLabel countLabel1 = new JLabel("0");
private JLabel statusLabel = new JLabel("Task not completed.");
private JButton startButton = new JButton("Start");
private ArrayList<Train> trainList = new ArrayList<>();
private ArrayList<Train> processedTrains = new ArrayList<>();

public MainFrame(String title) {
    super(title);

    setLayout(new GridBagLayout());

    countLabel1.setFont(new Font("serif", Font.BOLD, 28));

    GridBagConstraints gc = new GridBagConstraints();

    gc.fill = GridBagConstraints.NONE;

    gc.gridx = 0;
    gc.gridy = 0;
    gc.weightx = 1;
    gc.weighty = 1;
    add(countLabel1, gc);

    gc.gridx = 0;
    gc.gridy = 1;
    gc.weightx = 1;
    gc.weighty = 1;
    add(statusLabel, gc);

    gc.gridx = 0;
    gc.gridy = 2;
    gc.weightx = 1;
    gc.weighty = 1;
    add(startButton, gc);

    Train t1 = new Train();
    t1.setId(1);
    t1.setName("John");

    Train t2 = new Train();
    t2.setId(2);
    t2.setName("Mark");

    Train t3 = new Train();
    t3.setId(3);
    t3.setName("Lewis");


    trainList.add(t1);
    trainList.add(t2);
    trainList.add(t3);


    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            TrainMethods tm = new TrainMethods();

            // How can the swing worker return the array from the
            // processTrains method to this processedTrains array
            processedTrains = tm.processTrains(trainList);

            for(int i = 0; i<processedTrains.size(); i++)
            {
                System.out.println("Train id: " + processedTrains.get(i).getId() 
                        + " Train Name: " + processedTrains.get(i).getName());
            }
        }
    });

    setSize(200, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

private class Train
{
    String name;
    int id;

    public void setName(String name) {
        this.name = name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

}

private class TrainMethods
{
    public ArrayList processTrains(ArrayList<Train> trainList)
    {
        // Add here a SwingWorker to create a new thread
        // and execute the following code and update later on
        // a progress bar
        for(int i = 0; i<trainList.size(); i++)
        {
            trainList.get(i).setName("No name");
        }
        return trainList;
    }
}
}

我假装在新的processTrains swingworker中执行方法thread。如您所见,processTrains方法需要一个参数{Train \ {1}}。所以我的问题是,当我创建swing工作者时,如何将参数(在本例中为ArrayList ArrayList)传递给Train,如何返回进程{{1变量processedTrains,。

0 个答案:

没有答案