Question

I've a simple JDialog with JOptionPane and it works fine. But I want to use JFrame and create more complex window but I want it to act as JDialog, it means I need to halt code when JFrame or JDialog opens as next steps depends on what is chosen in window.

Here is my Class, is there anyway to run it so that it halts my code

public class MyFrame extends JDialog{
private static final long serialVersionUID = 1L;
public MyFrame() throws IOException {
    setSize(600, 450);
    dialogInit();
    setTitle("Travel");
    setVisible(true);
    URL url = this.getClass().getResource("/images/travel1.png");
    BufferedImage bf = ImageIO.read(url);
    this.setContentPane(new backImage(bf));
    JButton b = new JButton("Send");
    JCheckBox tf=new JCheckBox("Country");
    b.setBounds(318, 143, 98, 27);
    tf.setBounds(235, 104, 150, 27);
    add(b);
    add(tf);
}
}
Was it helpful?

Solution

What you want is for your dialog to be modal. Use Dialog.setModal(boolean):

public MyFrame() throws IOException {
    setModal(true);

Then when you call setVisible on the dialog from the code that constructed it, the code will wait until the dialog is closed.

OTHER TIPS

You can have your frame manually run the event pump until it closes. See this question.

In your code, you're still making a JDialog since MyFrame extends JDialog, which is what you probably want instead of a JFrame. What you need to do in addition is to make it modal.

Making your dialog via an appropriate constructor or by using setModal(true) will block input to other windows of your program and halt execution in the caller until the dialog is closed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top