tags:

views:

279

answers:

2

Hi, i have a small program where an element is draged and dropped, when the drop is performed i open a dialog (extends Jframe) where some text should be entered. The Problem is, that i want to wait for this dialog to be closed (actually the ok button to be pressed so i can read out the data from the textfield), than analyse what the user has entered and based on that i will decide if the drop is rejected or allowed.

public void drop(DropTargetDropEvent e) {
      try{
            //popup

            Popup p = new Popup();
            p.setParmeter("enter a new name: ");
            p.setVisible(true);

            //programm wont wait before the user has pressed ok in the popup 
            System.out.println("value: " + p.getValue());

            repaint();

        } else {
            e.rejectDrop();
        }
}

I hope you get the idea. Popup is a dialog extended from a JFrame. The problem is, that p.getValue() is executed before the User gets to press the ok button. I tried using a boolean variable and a loop to check if something was entered in the popup but it doesnt work, the dialog is desplayed but there is not textfield or ok button, so the only thing i can do is to kill it. I'm pretty new to gui's so i really would appreciate the help. Thanks in advance.

A: 

Java has built-in dialog support. Yon don't want to extend JFrame. See the tutorial on how to make dialogs.

SingleShot
+2  A: 

If possible you should re-implement Popup to inherit from JDialog instead of JFrame, and call JDialog's setModal(true) method, which will prevent subsequent code from running until the dialog is dismissed.

Alternatively, check out the static convenience methods in JOptionPane, which eliminate the need to implement your own bespoke dialog class in many cases. For example (from the JOptionPane API):

Show an information panel with the options yes/no and message 'choose one':

JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
Adamski