0

NOTE: SSCCE at the end of this question

I'm new to SwingWorker so please bear with me.

I'm using a GUI and when the user presses a button it executes some methods I've in another class. This class just has methods inside and I need those methods to run on their own thread. The thing is, those methods need to be passed arguments and they will also return something needed to other methods.

Something like this:

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;
   }

}

And my GUI Class:

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);
}

I'll also need to update the GUI and this is not an issue to me as I know I can use the publish() and override the process() method in the swing worker. My problem is how can I return the methods to the variables in the GUI and how can I pass arguments to the methods in MyClass.class, as doInBackground() does not accept arguments.

Also, I need the swingworker to be inside the method and not on the MyGUI.class, so something like this (eg for the processTrain method):

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();

If there is something missing and/or unclear please let me know, and I'll update the question.

//////////////////////////// ___EDIT____ ///////////////////////////////

As requested here is a short executable code:

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;
    }
}
}

What I pretend is to execute the method processTrains in a new swingworker thread. As you can see, the processTrains method requires a parameter, an ArrayList of Train. So my question is when I create the swing worker how can I pass the parameters, in this case the ArrayList of Train, to the doInBackground and how can I return the process ArrayList<Train> to the variable processedTrains,.


Programatic Notifications for "Find my iPhone"?

When "Find my iPhone" is invoked, is there a way for an app running on the receiving phone to receive a notification that the event had occurred?

4

0 に答える 0