vvolkgang
vvolkgang

Reputation: 471

Open java Applet or Frame inside a parent Applet method and wait for input

I have a Java applet (lets call it parentApplet) with a public method which must return information regarding the status of the performed actions (let's call it getUserInput()) . This method opens another Applet which needs user button input, by adding it as a child with add(childApplet), and afterwards adding itself (the parent) as an ActionListner of the buttons in the childApplet, being able to run other methods when the user clicks on the buttons in the childApplet.

My question is, how can I halt getUserInput() execution until the user has clicked the childApplet buttons?

I tried to have a static variable that tracks the return information, and spinning on a while(var == null) Thread.Sleep(1000); but it blocks the main thread, as it should.

PS: Having the childApplet as an applet can be changed to anything that could better fulfil the requirement of opening another panel on top of the parent applet.

Details on getUserInput()

That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and OK/Clear/Cancel buttons. When the user presses OK, I need to get the BufferedImage drawn. Do you know if this can be accomplished by extending a JDialog?

Upvotes: 0

Views: 492

Answers (2)

Peter Quiring
Peter Quiring

Reputation: 1706

You really need to restructure your app. You can't do it the way you want. Try creating a JDialog set it up as you like with input fields and an OK/Cancel button. Then to show the dialog do:

  MyDialog dialog = new MyDialog(null, true);  //true = modal
  //dialog.setModalityType(ModalityType.DOCUMENT_MODAL);  //or specify modal here
  dialog.setVisible(true);  //waits until dialog is closed
  if (dialog.wasAccepted()) {
    //grab values from dialog
    dialog.getCanvas();
  }

In the dialog you would have:

  private boolean accepted = false;
  public boolean wasAccepted() {return accepted;}
  public Canvas getCanvas() {return canvas;}
  public ? getWhateverElseYouWant() {return ...;}

The OK button would:

accepted=true;
dispose();

The Cancel button would:

accepted=false;
dispose();

The JDialog will pump events while it's visible. So the setVisible() function will halt execution until the dialog is closed.

That should work better, and then you can return many user input fields. You can even change the JDialog constructor to pass default value(s) in.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168815

That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and ok/clear/cancel buttons. When the user presses OK, I need to get the BufferedImage drawn.

First of all, it should not be an applet but a JPanel (it is not impossible to do it as an applet, but also not trivial). Then you can show the JPanel in a one of three ways.

  1. JOptionPane.showMessageDialog(..). The idea would be to use the ready made OK/Cancel buttons of the option pane to tell the main app. whether to actually use image drawn. I (as a user) would tend to expect the Clear button to not be in the group of buttons that dismisses the dialog. Put that button in the panel you pass to the option pane.
  2. A modal JDialog. Creating a dialog is more work than using an option pane, but also more versatile. For instance, if the panel you put into the dialog has a 'set size of drawing' option, it is easier to resize a dialog than an option pane.
  3. Another card of a CardLayout. The two previous suggestions have the dis/advantage that they will block the app. and the browser until dismissed. This is good in that you can simply query the state of the drawing immediately after it is shown, confident that the user has finished drawing. But it is bad in that the user might have the dialog or option pane sitting on screen for 30 minutes, and the rest of the browser will be inaccessible to them in that time. By instead flipping to a card that shows the drawing panel, the browser is not blocked, and the app. can query the state of the drawing as soon as the user makes one of the OK/Cancel selections.

Upvotes: 1

Related Questions