1

I wrote my program in two parts, I wrote the actual functionality first, and then the GUI to display it all. I need to wait/pause for a user to click a "Done" button from another class(DisplayImages) before continuing executing code. My DisplayImages class takes a list of MyImage. The images are then displayed in a jpanel and the user selects a couple of images and then clicks a 'Done' button. How can I wait for a response or something like that?

public class One{

    ArrayList<MyImage> images = new ArrayList<MyImage>();

    public One(){
        DisplayImages displayOne = new DisplayImages(images);
        displayOne.run();
        //I need to pause/wait here until the user has pressed the done button
        //in the DisplayImages class

        images.clear();
        images = displayOne.getSelectedImages();

        //do stuff here with images arrylist
        }
}    

DisplayImages class

public class DisplayImages extends JFrame{
    private ArrayList<MyImage> images = new ArrayList<MyImage>();
    private ArrayList<MyImage> selectedImages = new ArrayList<MyImage>();

    public DisplayImages(ArrayList<MyImage> images){
        this.images = images;
    }

    public void run(){
        //code creates a jpanel and displays images along with a done button

        //user presses done button
        done.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                selectedImages = getSelectedImages();
                //here I would send a signal to continue running in class One
            }
        });
    }

    private ArrayList<MyImage> getSelectedImages(){
        //code returns list of all the images the user selected on JPanel
        return results;
    }
}

3 Answers 3

2

If for some reason you need to open and process the dialog in the same method then using the dialog approach suggested with JOptionPane seems fine. However this seems bad design (opening a frame and waiting for input in a constructor?). I would prefer an approach similar to the one below (please read my inline comments):

public class One {

    ArrayList<MyImage> images = new ArrayList<MyImage>();

    public One() {
        // perform only initialization here
    }

    // call this method to create the dialog that allows the user to select images
    public void showDialog() {
        DisplayImages displayOne = new DisplayImages(images);

        // pass a reference to this object so DisplayImages can call it back
        displayOne.run(this);
    }

    // this will be called by the action listener of DisplayImages when Done is clicked
    public void processSelectedImages(List<MyImage> selectedImages) {
        images.clear();
        images = selectedImages;

        // do stuff here with images arrylist
    }
}


public class DisplayImages {
    ...
    public void run(final One callback){  // Note that now a reference to the caller is passed
         // creates jpanel and displays images along with a done button

         // user presses done button
         done.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e) {
                 setVisible(false);
                 selectedImages = getSelectedImages();

                 // here is how we can send a signal to notify the caller
                 callback.processSelectedImages(selectedImages); 
             }
         });
    }
    ...
}

As a side note please do not name your methods run() if you are not implementing the Runnable interface and/or using threads. This is very confusing

3
  • +1 Of course a listener is better than my suggestion but I wanted to keep it easy.
    – Stephan
    Commented Jul 23, 2013 at 8:46
  • This looks like something I might use. I might have to use Reflection since public void run(final One callback) is hardcoded to accept only type One classes but I will need other classes calling run. Commented Jul 23, 2013 at 22:39
  • @Miss_poker You could make all the classes like One implement an interface (let's say Caller) that would have one method public void processSelectedImages(List<MyImage> selectedImages). Then DisplayImages.run() would change to public void run(final Caller caller). If this is still not good enough for you please edit your question to provide clarifications about your restrictions
    – c.s.
    Commented Jul 23, 2013 at 22:50
1

That is quite easy, some people here are thinking to complicated. You will need no multithreading, you just need a modal dialog. JOptionPane provides easy access to them.

I modified your code:

public class One{

    ArrayList<MyImage> images = new ArrayList<MyImage>();

    public One(){
        DisplayImages displayOne = new DisplayImages(images);
        int n = JOptionPane.showConfirmDialog(null, displayOne);

        if (n == JOptionPane.OK_OPTION){
          //I need to pause/wait here until the user has pressed the done button
          //in the DisplayImages class

          images = displayOne.getSelectedImages();

          //do stuff here with images arrylist
          }
        }
}    

MyImage class

public class DisplayImages extends JPanel{
    private ArrayList<MyImage> images = new ArrayList<MyImage>();

    public DisplayImages(ArrayList<MyImage> images){
        this.images = images;

        //code creates a jpanel and displays images along with a done button
    }

    public ArrayList<MyImage> getSelectedImages(){
        //code returns list of all the images the user selected on JPanel
        return results;
    }
}
-1

You will have to use threading.

http://docs.oracle.com/javase/tutorial/essential/concurrency/

Then you can set up each instance on a separate thread, and either use the built in Object.notify and Object.wait

Or have yourself a global flag variable, static, available to both classes, which you change to notify the other class that the done button was clicked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.